Skip to content

Commit 6d8685f

Browse files
Merge pull request #52 from BDisp/value-tuple-removed
Fixes #51. Chess Symbols (U+1fa00-1fa0f) not returning Column Width bigger than 1.
2 parents b8a449c + 6b2b3d4 commit 6d8685f

File tree

9 files changed

+420
-62
lines changed

9 files changed

+420
-62
lines changed

NStack/NStack.csproj

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,8 @@ Added ustring.ColumnWidth to return number of columns that a ustring takes in a
4646
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
4747
<LangVersion>Latest</LangVersion>
4848
</PropertyGroup>
49-
<ItemGroup>
50-
<Reference Include="System.ValueTuple">
51-
<HintPath>..\packages\System.ValueTuple.4.3.1\lib\netstandard1.0\System.ValueTuple.dll</HintPath>
52-
<Private>False</Private>
53-
</Reference>
54-
</ItemGroup>
55-
<Import Project="..\packages\NuGet.Build.Packaging.0.1.248\build\NuGet.Build.Packaging.targets" Condition="Exists('..\packages\NuGet.Build.Packaging.0.1.248\build\NuGet.Build.Packaging.targets')" />
49+
<Import Project="..\packages\NuGet.Build.Packaging.0.1.248\build\NuGet.Build.Packaging.targets" Condition="Exists('..\packages\NuGet.Build.Packaging.0.1.248\build\NuGet.Build.Packaging.targets')" />
50+
<ItemGroup>
51+
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
52+
</ItemGroup>
5653
</Project>

NStack/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="NuGet.Build.Packaging" version="0.1.248" targetFramework="net461" developmentDependency="true" />
4-
<package id="System.ValueTuple" version="4.3.1" targetFramework="net461" />
4+
<package id="System.ValueTuple" version="4.5.0" targetFramework="net462" />
55
</packages>

NStack/strings/ustring.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ public static ustring Make (IEnumerable<Rune> runes)
481481
return Make (runes.ToList ());
482482
}
483483

484+
/// <summary>
484485
/// Initializes a new instance of the <see cref="T:NStack.ustring"/> class from an array of uints, which contain CodePoints.
485486
/// </summary>
486487
/// <returns>The make.</returns>
@@ -779,7 +780,7 @@ public bool Equals (ustring other)
779780
/// <summary>
780781
/// Reports whether this string and the provided string, when interpreted as UTF-8 strings, are equal under Unicode case-folding
781782
/// </summary>
782-
/// <returns><c>true</c>, if fold was equalsed, <c>false</c> otherwise.</returns>
783+
/// <returns><c>true</c>, if fold was equaled, <c>false</c> otherwise.</returns>
783784
/// <param name="other">Other.</param>
784785
public bool EqualsFold (ustring other)
785786
{
@@ -888,7 +889,7 @@ public static ustring Make (byte [] buffer, int start, int count)
888889
/// Returns the byte at the specified position.
889890
/// </summary>
890891
/// <value>The byte encoded at the specified position.</value>
891-
/// <remarks>The index value shoudl be between 0 and Length-1.</remarks>
892+
/// <remarks>The index value should be between 0 and Length-1.</remarks>
892893
public abstract byte this [int index] { get; }
893894

894895
/// <summary>
@@ -903,7 +904,7 @@ public static ustring Make (byte [] buffer, int start, int count)
903904
/// Returns a slice of the ustring delimited by the [start, end) range. If the range is invalid, the return is the Empty string.
904905
/// </summary>
905906
/// <param name="start">Start index, this value is inclusive. If the value is negative, the value is added to the length, allowing this parameter to count to count from the end of the string.</param>
906-
/// <param name="iend">End index, this value is exclusive. If the value is negative, the value is added to the length, plus one, allowing this parameter to count from the end of the string.</param>
907+
/// <param name="end">End index, this value is exclusive. If the value is negative, the value is added to the length, plus one, allowing this parameter to count from the end of the string.</param>
907908
/// <remarks>
908909
/// <para>
909910
/// Some examples given the string "1234567890":
@@ -1011,7 +1012,7 @@ public static ustring Make (byte [] buffer, int start, int count)
10111012
/// Utf8 encoded string.
10121013
/// </summary>
10131014
/// <returns>The substring starting at the specified offset.</returns>
1014-
/// <param name="start">Starting point, the value is .</param>
1015+
/// <param name="byteStart">Starting point, the value is .</param>
10151016
public ustring Substring (int byteStart)
10161017
{
10171018
int len = Length;
@@ -1166,6 +1167,7 @@ public List<Rune> ToRuneList ()
11661167
return result;
11671168
}
11681169

1170+
/// <summary>
11691171
/// Converts a ustring into a rune array.
11701172
/// </summary>
11711173
/// <returns>An array containing the runes for the string up to the specified limit.</returns>
@@ -1541,7 +1543,7 @@ public int IndexOfAny (params uint [] runes)
15411543
}
15421544

15431545
/// <summary>
1544-
/// Reports the zero-based index position of the last occurrence in this instance of one or more characters specified in the uustring.
1546+
/// Reports the zero-based index position of the last occurrence in this instance of one or more characters specified in the ustring.
15451547
/// </summary>
15461548
/// <returns>The index position of the last occurrence in this instance where any character in <paramref name="chars" /> was found; -1 if no character in <paramref name="chars" /> was found.</returns>
15471549
/// <param name="chars">The string containing characters to seek.</param>
@@ -2170,7 +2172,7 @@ public ustring Replace (ustring oldValue, ustring newValue, int maxReplacements
21702172
var oldLen = oldValue.Length;
21712173
var newLen = newValue.Length;
21722174

2173-
// Apply replcements to buffer
2175+
// Apply replacements to buffer
21742176
var result = new byte [Length + maxReplacements * (newValue.Length - oldValue.Length)];
21752177
int w = 0, start = 0;
21762178
for (int i = 0; i < maxReplacements; i++) {
@@ -2196,6 +2198,11 @@ public ustring Replace (ustring oldValue, ustring newValue, int maxReplacements
21962198
return new ByteBufferUString (result);
21972199
}
21982200

2201+
/// <summary>
2202+
/// Represent the null or empty value related to the ustring.
2203+
/// </summary>
2204+
/// <param name="value"></param>
2205+
/// <returns></returns>
21992206
public static bool IsNullOrEmpty (ustring value)
22002207
{
22012208
if (value == null)

NStack/unicode/Graphic.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ internal enum CharClass : byte {
3333
/// <summary>
3434
/// Determines if a rune is on a set of ranges.
3535
/// </summary>
36-
/// <returns><c>true</c>, if rune in ranges was ised, <c>false</c> otherwise.</returns>
36+
/// <returns><c>true</c>, if rune in ranges was used, <c>false</c> otherwise.</returns>
3737
/// <param name="rune">Rune.</param>
3838
/// <param name="inRanges">In ranges.</param>
3939
public static bool IsRuneInRanges (uint rune, params RangeTable [] inRanges)
@@ -47,7 +47,7 @@ public static bool IsRuneInRanges (uint rune, params RangeTable [] inRanges)
4747
/// <summary>
4848
/// IsGraphic reports whether the rune is defined as a Graphic by Unicode.
4949
/// </summary>
50-
/// <returns><c>true</c>, if the rune is a lower case lette, <c>false</c> otherwise.</returns>
50+
/// <returns><c>true</c>, if the rune is a lower case letter, <c>false</c> otherwise.</returns>
5151
/// <param name="rune">The rune to test for.</param>
5252
/// <remarks>
5353
/// Such characters include letters, marks, numbers, punctuation, symbols, and
@@ -63,7 +63,7 @@ public static bool IsGraphic (uint rune)
6363
/// <summary>
6464
/// IsPrint reports whether the rune is defined as printable.
6565
/// </summary>
66-
/// <returns><c>true</c>, if the rune is a lower case lette, <c>false</c> otherwise.</returns>
66+
/// <returns><c>true</c>, if the rune is a lower case letter, <c>false</c> otherwise.</returns>
6767
/// <param name="rune">The rune to test for.</param>
6868
/// <remarks>
6969
/// Such characters include letters, marks, numbers, punctuation, symbols, and the
@@ -81,7 +81,7 @@ public static bool IsPrint (uint rune)
8181
/// <summary>
8282
/// IsControl reports whether the rune is a control character.
8383
/// </summary>
84-
/// <returns><c>true</c>, if the rune is a lower case lette, <c>false</c> otherwise.</returns>
84+
/// <returns><c>true</c>, if the rune is a lower case letter, <c>false</c> otherwise.</returns>
8585
/// <param name="rune">The rune to test for.</param>
8686
/// <remarks>
8787
/// The C (Other) Unicode category includes more code points such as surrogates; use C.InRange (r) to test for them.

NStack/unicode/Rune.ColumnWidth.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,16 @@ public static int ColumnWidth (Rune rune)
9898
return 1 +
9999
((irune >= 0x1100 &&
100100
(irune <= 0x115f || /* Hangul Jamo init. consonants */
101-
irune == 0x2329 || irune == 0x232a ||
101+
irune == 0x2329 || irune == 0x232a || /* Miscellaneous Technical */
102102
(irune >= 0x2e80 && irune <= 0xa4cf &&
103-
irune != 0x303f) || /* CJK ... Yi */
103+
irune != 0x303f) || /* CJK ... Yi */
104104
(irune >= 0xac00 && irune <= 0xd7a3) || /* Hangul Syllables */
105105
(irune >= 0xf900 && irune <= 0xfaff) || /* CJK Compatibility Ideographs */
106106
(irune >= 0xfe10 && irune <= 0xfe19) || /* Vertical forms */
107107
(irune >= 0xfe30 && irune <= 0xfe6f) || /* CJK Compatibility Forms */
108108
(irune >= 0xff00 && irune <= 0xff60) || /* Fullwidth Forms */
109-
(irune >= 0xffe0 && irune <= 0xffe6) ||
109+
(irune >= 0xffe0 && irune <= 0xffe6) || /* Alphabetic Presentation Forms*/
110+
(irune >= 0x1fa00 && irune <= 0x1facf) || /* Chess Symbols*/
110111
(irune >= 0x20000 && irune <= 0x2fffd) ||
111112
(irune >= 0x30000 && irune <= 0x3fffd))) ? 1 : 0);
112113
}

NStack/unicode/Rune.cs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public partial struct Rune {
3131
/// <summary>
3232
/// Represents invalid code points.
3333
/// </summary>
34-
public static Rune ReplacementChar = new Rune (0xfffd);
34+
public static Rune ReplacementChar = new Rune (0xfffd);
3535

3636
/// <summary>
3737
/// Maximum number of bytes required to encode every unicode code point.
@@ -44,7 +44,7 @@ public partial struct Rune {
4444
/// <param name="rune">Unsigned integer.</param>
4545
/// <remarks>
4646
/// The value does not have to be a valid Unicode code point, this API
47-
/// will create an instanceof Rune regardless of the whether it is in
47+
/// will create an instance of Rune regardless of the whether it is in
4848
/// range or not.
4949
/// </remarks>
5050
public Rune (uint rune)
@@ -76,21 +76,32 @@ public Rune (char ch)
7676
/// <param name="sgateMax">The low surrogate code points maximum value.</param>
7777
public Rune (uint sgateMin, uint sgateMax)
7878
{
79-
if (sgateMin < surrogateMin || sgateMax > surrogateMax)
79+
var rune = DecodeSurrogatePair (sgateMin, sgateMax);
80+
if (rune > 0)
81+
{
82+
this.value = rune;
83+
}
84+
else
8085
{
8186
throw new ArgumentOutOfRangeException($"Must be between {surrogateMin:x} and {surrogateMax:x} inclusive!");
8287
}
83-
this.value = DecodeSurrogatePair(sgateMin, sgateMax);
8488
}
8589

8690
/// <summary>
87-
/// Gets a value indicating whether this <see cref="T:System.Rune"/> can be encoded as UTF-8 from a surrogate pair.
91+
/// Gets a value indicating whether this <see cref="T:System.Rune"/> can be encoded as UTF-8 from a surrogate pair or zero otherwise.
8892
/// </summary>
8993
/// <param name="sgateMin">The high surrogate code points minimum value.</param>
9094
/// <param name="sgateMax">The low surrogate code points maximum value.</param>
91-
public static uint DecodeSurrogatePair(uint sgateMin, uint sgateMax)
95+
public static uint DecodeSurrogatePair (uint sgateMin, uint sgateMax)
9296
{
93-
return 0x10000 + ((sgateMin - surrogateMin) * 0x0400) + (sgateMax - lowSurrogateMin);
97+
if (sgateMin < surrogateMin || sgateMax > surrogateMax)
98+
{
99+
return 0;
100+
}
101+
else
102+
{
103+
return 0x10000 + ((sgateMin - surrogateMin) * 0x0400) + (sgateMax - lowSurrogateMin);
104+
}
94105
}
95106

96107
/// <summary>
@@ -229,7 +240,7 @@ public static bool FullRune (byte [] p)
229240
/// </returns>
230241
/// <param name="buffer">Byte buffer containing the utf8 string.</param>
231242
/// <param name="start">Starting offset to look into..</param>
232-
/// <param name="n">Number of bytes valid in the buffer, or -1 to make it the lenght of the buffer.</param>
243+
/// <param name="n">Number of bytes valid in the buffer, or -1 to make it the length of the buffer.</param>
233244
public static (Rune rune, int Size) DecodeRune (byte [] buffer, int start = 0, int n = -1)
234245
{
235246
if (buffer == null)
@@ -294,7 +305,7 @@ public static (Rune rune, int Size) DecodeRune (byte [] buffer, int start = 0, i
294305
/// it returns (RuneError, 0). Otherwise, if
295306
/// the encoding is invalid, it returns (RuneError, 1). Both are impossible
296307
/// results for correct, non-empty UTF-8.</param>
297-
/// <param name="end">Scan up to that point, if the value is -1, it sets the value to the lenght of the buffer.</param>
308+
/// <param name="end">Scan up to that point, if the value is -1, it sets the value to the length of the buffer.</param>
298309
/// <remarks>
299310
/// An encoding is invalid if it is incorrect UTF-8, encodes a rune that is
300311
/// out of range, or is not the shortest possible UTF-8 encoding for the
@@ -402,7 +413,7 @@ public static int EncodeRune (Rune rune, byte [] dest, int offset = 0)
402413
/// <summary>
403414
/// Returns the number of runes in a utf8 encoded buffer
404415
/// </summary>
405-
/// <returns>Numnber of runes.</returns>
416+
/// <returns>Number of runes.</returns>
406417
/// <param name="buffer">Byte buffer containing a utf8 string.</param>
407418
/// <param name="offset">Starting offset in the buffer.</param>
408419
/// <param name="count">Number of bytes to process in buffer, or -1 to process until the end of the buffer.</param>
@@ -476,7 +487,7 @@ public static bool Valid (byte [] buffer)
476487
/// <summary>
477488
/// Use to find the index of the first invalid utf8 byte sequence in a buffer
478489
/// </summary>
479-
/// <returns>The index of the first insvalid byte sequence or -1 if the entire buffer is valid.</returns>
490+
/// <returns>The index of the first invalid byte sequence or -1 if the entire buffer is valid.</returns>
480491
/// <param name="buffer">Buffer containing the utf8 buffer.</param>
481492
public static int InvalidIndex (byte [] buffer)
482493
{
@@ -526,7 +537,7 @@ public static int InvalidIndex (byte [] buffer)
526537
/// <summary>
527538
/// ValidRune reports whether a rune can be legally encoded as UTF-8.
528539
/// </summary>
529-
/// <returns><c>true</c>, if rune was valided, <c>false</c> otherwise.</returns>
540+
/// <returns><c>true</c>, if rune was validated, <c>false</c> otherwise.</returns>
530541
/// <param name="rune">The rune to test.</param>
531542
public static bool ValidRune (Rune rune)
532543
{
@@ -547,7 +558,7 @@ public static bool ValidRune (Rune rune)
547558
/// <summary>
548559
/// IsGraphic reports whether the rune is defined as a Graphic by Unicode.
549560
/// </summary>
550-
/// <returns><c>true</c>, if the rune is a lower case lette, <c>false</c> otherwise.</returns>
561+
/// <returns><c>true</c>, if the rune is a lower case letter, <c>false</c> otherwise.</returns>
551562
/// <param name="rune">The rune to test for.</param>
552563
/// <remarks>
553564
/// Such characters include letters, marks, numbers, punctuation, symbols, and
@@ -558,7 +569,7 @@ public static bool ValidRune (Rune rune)
558569
/// <summary>
559570
/// IsPrint reports whether the rune is defined as printable.
560571
/// </summary>
561-
/// <returns><c>true</c>, if the rune is a lower case lette, <c>false</c> otherwise.</returns>
572+
/// <returns><c>true</c>, if the rune is a lower case letter, <c>false</c> otherwise.</returns>
562573
/// <param name="rune">The rune to test for.</param>
563574
/// <remarks>
564575
/// Such characters include letters, marks, numbers, punctuation, symbols, and the
@@ -572,7 +583,7 @@ public static bool ValidRune (Rune rune)
572583
/// <summary>
573584
/// IsControl reports whether the rune is a control character.
574585
/// </summary>
575-
/// <returns><c>true</c>, if the rune is a lower case lette, <c>false</c> otherwise.</returns>
586+
/// <returns><c>true</c>, if the rune is a lower case letter, <c>false</c> otherwise.</returns>
576587
/// <param name="rune">The rune to test for.</param>
577588
/// <remarks>
578589
/// The C (Other) Unicode category includes more code points such as surrogates; use C.InRange (r) to test for them.
@@ -598,7 +609,7 @@ public static bool ValidRune (Rune rune)
598609
public static bool IsLetterOrDigit (Rune rune) => NStack.Unicode.IsLetter (rune.value) || NStack.Unicode.IsDigit (rune.value);
599610

600611
/// <summary>
601-
/// IsLetterOrDigit reports whether the rune is a letter (category L) or a number (caetegory N).
612+
/// IsLetterOrDigit reports whether the rune is a letter (category L) or a number (category N).
602613
/// </summary>
603614
/// <returns><c>true</c>, if the rune is a letter or number, <c>false</c> otherwise.</returns>
604615
/// <param name="rune">The rune to test for.</param>
@@ -658,21 +669,21 @@ public static bool ValidRune (Rune rune)
658669
/// <summary>
659670
/// Reports whether the rune is an upper case letter.
660671
/// </summary>
661-
/// <returns><c>true</c>, if the rune is an upper case lette, <c>false</c> otherwise.</returns>
672+
/// <returns><c>true</c>, if the rune is an upper case letter, <c>false</c> otherwise.</returns>
662673
/// <param name="rune">The rune to test for.</param>
663674
public static bool IsUpper (Rune rune) => NStack.Unicode.IsUpper (rune.value);
664675

665676
/// <summary>
666677
/// Reports whether the rune is a lower case letter.
667678
/// </summary>
668-
/// <returns><c>true</c>, if the rune is a lower case lette, <c>false</c> otherwise.</returns>
679+
/// <returns><c>true</c>, if the rune is a lower case letter, <c>false</c> otherwise.</returns>
669680
/// <param name="rune">The rune to test for.</param>
670681
public static bool IsLower (Rune rune) => NStack.Unicode.IsLower (rune.value);
671682

672683
/// <summary>
673684
/// Reports whether the rune is a title case letter.
674685
/// </summary>
675-
/// <returns><c>true</c>, if the rune is a lower case lette, <c>false</c> otherwise.</returns>
686+
/// <returns><c>true</c>, if the rune is a lower case letter, <c>false</c> otherwise.</returns>
676687
/// <param name="rune">The rune to test for.</param>
677688
public static bool IsTitle (Rune rune) => NStack.Unicode.IsTitle (rune.value);
678689

@@ -691,9 +702,9 @@ public enum Case {
691702
Lower = 1,
692703

693704
/// <summary>
694-
/// Titlecase capitalizes the first letter, and keeps the rest in lowercase.
705+
/// Title case capitalizes the first letter, and keeps the rest in lowercase.
695706
/// Sometimes it is not as straight forward as the uppercase, some characters require special handling, like
696-
/// certain ligatures and greek characters.
707+
/// certain ligatures and Greek characters.
697708
/// </summary>
698709
Title = 2
699710
};

NStack/unicode/Rune.extensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static bool FullRune (ustring str)
4646
/// </returns>
4747
/// <param name="str">ustring to decode.</param>
4848
/// <param name="start">Starting offset to look into..</param>
49-
/// <param name="n">Number of bytes valid in the buffer, or -1 to make it the lenght of the buffer.</param>
49+
/// <param name="n">Number of bytes valid in the buffer, or -1 to make it the length of the buffer.</param>
5050
public static (Rune rune, int size) DecodeRune (ustring str, int start = 0, int n = -1)
5151
{
5252
if ((object)str == null)
@@ -106,7 +106,7 @@ public static (Rune rune, int size) DecodeRune (ustring str, int start = 0, int
106106
/// it returns (RuneError, 0). Otherwise, if
107107
/// the encoding is invalid, it returns (RuneError, 1). Both are impossible
108108
/// results for correct, non-empty UTF-8.</param>
109-
/// <param name="end">Scan up to that point, if the value is -1, it sets the value to the lenght of the buffer.</param>
109+
/// <param name="end">Scan up to that point, if the value is -1, it sets the value to the length of the buffer.</param>
110110
/// <remarks>
111111
/// An encoding is invalid if it is incorrect UTF-8, encodes a rune that is
112112
/// out of range, or is not the shortest possible UTF-8 encoding for the
@@ -153,7 +153,7 @@ public static (Rune rune, int size) DecodeLastRune (ustring str, int end = -1)
153153
/// <summary>
154154
/// Returns the number of runes in a ustring.
155155
/// </summary>
156-
/// <returns>Numnber of runes.</returns>
156+
/// <returns>Number of runes.</returns>
157157
/// <param name="str">utf8 string.</param>
158158
public static int RuneCount (ustring str)
159159
{
@@ -216,7 +216,7 @@ public static int RuneCount (ustring str)
216216
/// <summary>
217217
/// Use to find the index of the first invalid utf8 byte sequence in a buffer
218218
/// </summary>
219-
/// <returns>The index of the first insvalid byte sequence or -1 if the entire buffer is valid.</returns>
219+
/// <returns>The index of the first invalid byte sequence or -1 if the entire buffer is valid.</returns>
220220
/// <param name="str">String containing the utf8 buffer.</param>
221221
public static int InvalidIndex (ustring str)
222222
{

0 commit comments

Comments
 (0)