Skip to content

Commit 1c9d0a2

Browse files
committed
Some helper methods
1 parent 358fcaa commit 1c9d0a2

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

NStack/strings/ustring.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using System.Text;
2121
using System.Runtime.InteropServices;
2222
using System.Collections;
23+
using System.Linq;
2324

2425
namespace NStack {
2526

@@ -470,6 +471,16 @@ public static ustring Make (IList<Rune> runes)
470471
return Make (encoded);
471472
}
472473

474+
/// <summary>
475+
/// Initializes a new instance of the <see cref="T:NStack.ustring"/> class from an IEnumerable of runes
476+
/// </summary>
477+
/// <returns>The make.</returns>
478+
/// <param name="runes">Runes.</param>
479+
public static ustring Make (IEnumerable<Rune> runes)
480+
{
481+
return Make (runes.ToList ());
482+
}
483+
473484
/// <summary>
474485
/// Initializes a new instance of the <see cref="T:NStack.ustring"/> class from a block of memory and a size.
475486
/// </summary>
@@ -1110,6 +1121,21 @@ public uint [] ToRunes (int limit = -1)
11101121
return result;
11111122
}
11121123

1124+
/// <summary>
1125+
/// Converts a ustring into a List of runes.
1126+
/// </summary>
1127+
/// <returns>A list containing the runes for the string, it is not bound by any limits.</returns>
1128+
public List<Rune> ToRuneList ()
1129+
{
1130+
var result = new List<Rune> ();
1131+
for (int offset = 0; offset < Length; ) {
1132+
(var rune, var size) = Utf8.DecodeRune (this, offset);
1133+
result.Add (rune);
1134+
offset += size;
1135+
}
1136+
return result;
1137+
}
1138+
11131139
// primeRK is the prime base used in Rabin-Karp algorithm.
11141140
const uint primeRK = 16777619;
11151141

@@ -1763,6 +1789,16 @@ public static bool Contains (ref AsciiSet aset, byte b)
17631789
yield break;
17641790
}
17651791

1792+
/// <summary>
1793+
/// Returns the Rune encoded at the specified byte <paramref name="index"/>.
1794+
/// </summary>
1795+
/// <returns>The <see cref="T:System.Rune"/> which might be Rune.Error if the value at the specified index is not UTF8 compliant, for example because it is not a valid UTF8 encoding, or the buffer is too short.</returns>
1796+
/// <param name="index">Index.</param>
1797+
public Rune RuneAt (int index)
1798+
{
1799+
return Utf8.DecodeRune (this, index).Rune;
1800+
}
1801+
17661802
// Map returns a copy of the string s with all its characters modified
17671803
// according to the mapping function. If mapping returns a negative value, the character is
17681804
// dropped from the string with no replacement.

0 commit comments

Comments
 (0)