https://github.com/sharpjs/unindent
String unindenter for .NET
https://github.com/sharpjs/unindent
dotnet string unindent
Last synced: 10 months ago
JSON representation
String unindenter for .NET
- Host: GitHub
- URL: https://github.com/sharpjs/unindent
- Owner: sharpjs
- License: isc
- Created: 2021-05-08T16:59:04.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-11-13T00:59:48.000Z (about 3 years ago)
- Last Synced: 2025-02-25T12:48:38.297Z (11 months ago)
- Topics: dotnet, string, unindent
- Language: C#
- Homepage:
- Size: 102 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Unindent
A tiny .NET library that removes indentation from multi-line strings.
## Status
[](https://github.com/sharpjs/Unindent/actions)
[](https://www.nuget.org/packages/Unindent)
[](https://www.nuget.org/packages/Unindent)
Test coverage is 100%.
Unindent is available as a [NuGet package](https://www.nuget.org/packages/Unindent).
## Usage
This package provides an `Unindent` extension method for .NET strings.
```csharp
var result = input.Unindent();
```
The `Unindent` method returns a string with the content of the input string,
but unindented: if all[1](#blank-lines) input lines begin with the
same amount of space (the *indentation*), that space is removed from the
returned string.
*Space* here means any mixture of the space (`U+0020`) and tab (`U+0009`)
characters. Tabs work like in most text editors:[2](#split-tabs) a
tab advances to the next tab stop. By default, tab stops are 8 spaces apart.
The optional `tabStop` parameter overrides the default.
Lines end via any mixture of the carriage return (`U+000D`) and line feed
(`U+000A`) characters. Thus Unindent supports `CR+LF` (Windows), `LF`
(Linux/Unix), and `CR` (classic Mac OS) line endings.
Click here for details about some edge cases.
- 1 Unindent ignores *blank* lines (those
containing only space) when discovering indentation in the input string, but
the method still removes indentation from blank lines that have it. See
[this test](https://github.com/sharpjs/Unindent/blob/4bad5c2249c4e4a4a4976ede12799e0d825bca61/Unindent.Tests/StringExtensionsTests.cs#L155-L158)
for an example.
- 2 If a tab character jumps past the
computed indentation width, that tab is replaced by space characters in order
to preserve column alignments present in the input string. See
[this test](https://github.com/sharpjs/Unindent/blob/4bad5c2249c4e4a4a4976ede12799e0d825bca61/Unindent.Tests/StringExtensionsTests.cs#L215)
for an example.
- If the input string ends with trailing space, Unindent removes that space.
See [this test](https://github.com/sharpjs/Unindent/blob/4bad5c2249c4e4a4a4976ede12799e0d825bca61/Unindent.Tests/StringExtensionsTests.cs#L155-L158)
for an example.
### Example
Given this example code:
```csharp
namespace Foo
{
public class Bar
{
const string Query = @"
-- Pretend there is a table called NaturalNumbers
SELECT TOP 100
Output = CASE
WHEN N % 15 = 0 THEN N'Fizz Buzz'
WHEN N % 5 = 0 THEN N'Buzz'
WHEN N % 3 = 0 THEN N'Fizz'
ELSE CONVERT(nvarchar(10), N)
END
FROM NaturalNumbers; -- 1, 2, 3, ...
";
}
}
```
The string constant `Query` is indented by 12 spaces. To unindent it:
```csharp
var query = Query.Unindent();
```
...which yields:
```
-- Pretend there is a table called NaturalNumbers
SELECT TOP 100
Output = CASE
WHEN N % 15 = 0 THEN N'Fizz Buzz'
WHEN N % 5 = 0 THEN N'Buzz'
WHEN N % 3 = 0 THEN N'Fizz'
ELSE CONVERT(nvarchar(10), N)
END
FROM NaturalNumbers; -- 1, 2, 3, ...
```