| | 1 | | namespace Kestrun.Utilities; |
| | 2 | | /// <summary> |
| | 3 | | /// Provides utility methods for Kestrun. |
| | 4 | | /// </summary> |
| | 5 | | public static class CcUtilities |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// Determines whether preview features are enabled in the current AppContext. |
| | 9 | | /// </summary> |
| | 10 | | public static bool PreviewFeaturesEnabled() => |
| 6 | 11 | | AppContext.TryGetSwitch( |
| 6 | 12 | | "System.Runtime.EnablePreviewFeatures", out var on) && on; |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// Returns the line number in the source string at the specified character index. |
| | 16 | | /// </summary> |
| | 17 | | /// <param name="source">The source string to search.</param> |
| | 18 | | /// <param name="index">The character index for which to find the line number.</param> |
| | 19 | | /// <returns>The line number at the specified index.</returns> |
| | 20 | | /// <exception cref="ArgumentOutOfRangeException">Thrown when index is out of range.</exception> |
| | 21 | | public static int GetLineNumber(string source, int index) |
| | 22 | | { |
| 17 | 23 | | if (index < 0 || index > source.Length) |
| | 24 | | { |
| 2 | 25 | | throw new ArgumentOutOfRangeException(nameof(index)); |
| | 26 | | } |
| | 27 | |
|
| | 28 | | // Count how many `\n` occur before the index |
| 15 | 29 | | var line = 1; |
| 12716 | 30 | | for (var i = 0; i < index; i++) |
| | 31 | | { |
| 6343 | 32 | | if (source[i] == '\n') |
| | 33 | | { |
| 165 | 34 | | line++; |
| | 35 | | } |
| | 36 | | } |
| 15 | 37 | | return line; |
| | 38 | | } |
| | 39 | | } |