|
| 1 | +## About |
| 2 | + |
| 3 | +Provides `Index` and `Range` types to simplify slicing operations on collections for .NET Framework and .NET Standard 2.0. |
| 4 | +Provides `Base64Url` for encoding data in a URL-safe manner on .NET Framework and .NET Standard. |
| 5 | + |
| 6 | +This library is not necessary nor recommended when targeting versions of .NET that include the relevant support. |
| 7 | + |
| 8 | +## Key Features |
| 9 | + |
| 10 | +<!-- The key features of this package --> |
| 11 | + |
| 12 | +* Enables the use of `Index` and `Range` types on older .NET platforms. |
| 13 | +* Provides `Base64Url` encoding, decoding, and validation for URL-safe data processing on older .NET platforms. |
| 14 | + |
| 15 | +## How to Use |
| 16 | + |
| 17 | +<!-- A compelling example on how to use this package with code, as well as any specific guidelines for when to use the package --> |
| 18 | + |
| 19 | +The `Index` and `Range` types simplify working with slices of arrays, strings, or other collections. |
| 20 | + |
| 21 | +```csharp |
| 22 | +string[] words = ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"]; |
| 23 | + |
| 24 | +// Use Index to reference the last element |
| 25 | +Console.WriteLine(words[^1]); |
| 26 | +// Output: "dog" |
| 27 | +
|
| 28 | +// Use Range to reference a slice |
| 29 | +string[] phrase = words[1..4]; |
| 30 | +Console.WriteLine(string.Join(" ", phrase)); |
| 31 | +// Output: "quick brown fox" |
| 32 | +``` |
| 33 | + |
| 34 | +`Base64Url` encoding is a URL-safe version of Base64, commonly used in web applications, such as JWT tokens. |
| 35 | + |
| 36 | +```csharp |
| 37 | +using System.Buffers.Text; |
| 38 | +using System.Text; |
| 39 | + |
| 40 | +// Original data |
| 41 | +byte[] data = Encoding.UTF8.GetBytes("Hello World!"); |
| 42 | + |
| 43 | +Span<byte> encoded = new byte[Base64Url.GetEncodedLength(data.Length)]; |
| 44 | +Base64Url.EncodeToUtf8(data, encoded, out int _, out int bytesWritten); |
| 45 | + |
| 46 | +string encodedString = Base64Url.EncodeToString(data); |
| 47 | +Console.WriteLine($"Encoded: {encodedString}"); |
| 48 | +// Encoded: SGVsbG8gV29ybGQh |
| 49 | +
|
| 50 | +Span<byte> decoded = new byte[data.Length]; |
| 51 | +Base64Url.DecodeFromUtf8(encoded[..bytesWritten], decoded, out _, out bytesWritten); |
| 52 | + |
| 53 | +string decodedString = Encoding.UTF8.GetString(decoded[..bytesWritten]); |
| 54 | +Console.WriteLine($"Decoded: {decodedString}"); |
| 55 | +// Decoded: Hello World! |
| 56 | +``` |
| 57 | + |
| 58 | +## Main Types |
| 59 | + |
| 60 | +<!-- The main types provided in this library --> |
| 61 | + |
| 62 | +The main types provided by this library are: |
| 63 | + |
| 64 | +* `System.Index` |
| 65 | +* `System.Range` |
| 66 | +* `System.Buffers.Text.Base64Url` |
| 67 | + |
| 68 | +## Additional Documentation |
| 69 | + |
| 70 | +<!-- Links to further documentation. Remove conceptual documentation if not available for the library. --> |
| 71 | + |
| 72 | +API documentation |
| 73 | + |
| 74 | +* [System.Index](https://learn.microsoft.com/dotnet/api/system.index) |
| 75 | +* [System.Range](https://learn.microsoft.com/dotnet/api/system.range) |
| 76 | +* [System.Buffers.Text.Base64Url](https://learn.microsoft.com/dotnet/api/system.buffers.text.base64url) |
| 77 | + |
| 78 | +## Feedback & Contributing |
| 79 | + |
| 80 | +<!-- How to provide feedback on this package and contribute to it --> |
| 81 | + |
| 82 | +Microsoft.Bcl.Memory is released as open source under the [MIT license](https://licenses.nuget.org/MIT). |
| 83 | +Bug reports and contributions are welcome at [the GitHub repository](https://github.com/dotnet/runtime). |
0 commit comments