Skip to content

Commit e489e28

Browse files
committed
Introduce new this[int,object] to allow to differentiate from this[int,int] to mean 'Until the last value of the string', which was previously overloaded on the value zero for 'end'
1 parent 3316832 commit e489e28

File tree

4 files changed

+81
-23
lines changed

4 files changed

+81
-23
lines changed

NStack/NStack.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<PropertyGroup>
66
<TargetFramework>netstandard1.5</TargetFramework>
77
<PackageId>NStack.Core</PackageId>
8-
<PackageVersion>0.6</PackageVersion>
8+
<PackageVersion>0.7</PackageVersion>
99
<Authors>Miguel de Icaza</Authors>
1010
<PackageLicenseUrl>https://github.com/migueldeicaza/NStack/blob/master/LICENSE.md</PackageLicenseUrl>
1111
<Owners>Miguel de Icaza</Owners>
@@ -19,7 +19,7 @@ It starts with a new string type that is focused on Unicode code-points as oppos
1919
<ReleaseVersion>0.3</ReleaseVersion>
2020
<PackageReleaseNotes>* Renamed some methods to match the equivalent methods in Char.
2121
* Introduced Substring (int start)
22-
* Typo fix</PackageReleaseNotes>
22+
* Introduced difference between this [int start, int end] and this [int start, object end] the latter used to mean "until the end", replacing '0' as the previous value for "until the end".</PackageReleaseNotes>
2323
</PropertyGroup>
2424

2525
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">

NStack/strings/ustring.cs

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -850,20 +850,21 @@ public static ustring Make (byte [] buffer, int start, int count)
850850
/// Returns a slice of the ustring delimited by the [start, end) range. If the range is invalid, the return is the Empty string.
851851
/// </summary>
852852
/// <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>
853-
/// <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. If the value is zero, the end is computed as the last index of the string.</param>
853+
/// <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>
854854
/// <remarks>
855855
/// <para>
856856
/// Some examples given the string "1234567890":
857857
/// </para>
858858
/// <para>The range [0, 4] produces "1234"</para>
859859
/// <para>The range [8, 10] produces "90"</para>
860-
/// <para>The range [8, 0] produces "90"</para>
861-
/// <para>The range [-2, 0] produces "90"</para>
860+
/// <para>The range [8, null] produces "90"</para>
861+
/// <para>The range [-2, null] produces "90"</para>
862862
/// <para>The range [8, 9] produces "9"</para>
863863
/// <para>The range [-4, -1] produces "789"</para>
864-
/// <para>The range [-4, 0] produces "7890"</para>
865-
/// <para>The range [-4, 0] produces "7890"</para>
864+
/// <para>The range [-4, null] produces "7890"</para>
865+
/// <para>The range [-4, null] produces "7890"</para>
866866
/// <para>The range [-9, -3] produces "234567"</para>
867+
/// <para>The range [0, 0] produces the empty string</para>
867868
/// <para>
868869
/// This indexer does not raise exceptions for invalid indexes, instead the value
869870
/// returned is the ustring.Empty value:
@@ -874,16 +875,18 @@ public static ustring Make (byte [] buffer, int start, int count)
874875
/// <para>
875876
/// The range [-100, 0] produces ustring.Empty
876877
/// </para>
878+
/// <para>
879+
/// To simulate the optional end boundary, use the indexer that takes the
880+
/// object parameter and pass a null to it. For example, to fetch all
881+
/// elements from the position five until the end, use [5, null]
882+
/// </para>
877883
/// </remarks>
878884
public ustring this [int start, int end] {
879885
get {
880886
int size = Length;
881-
if (end < 0) {
882-
if (end == 0)
883-
end = size;
884-
else
885-
end = size + end;
886-
}
887+
if (end < 0)
888+
end = size + end;
889+
887890
if (start < 0)
888891
start = size + start;
889892

@@ -895,6 +898,57 @@ public static ustring Make (byte [] buffer, int start, int count)
895898
}
896899
}
897900

901+
/// <summary>
902+
/// Returns a slice of the ustring delimited by the [start, last-element-of-the-string range. If the range is invalid, the return is the Empty string.
903+
/// </summary>
904+
/// <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>
905+
/// <param name="end">This value is expected to be null to indicate that it should be the last element of the string.</param>
906+
/// <remarks>
907+
/// <para>
908+
/// This is a companion indexer to the indexer that takes two integers, it only exists
909+
/// to provide the optional end argument to mean "until the end", and to make the code
910+
/// that uses indexer look familiar, without having to resort to another API.
911+
///
912+
/// Some examples given the string "1234567890":
913+
/// </para>
914+
/// <para>The range [8, null] produces "90"</para>
915+
/// <para>The range [-2, null] produces "90"</para>
916+
/// <para>The range [8, 9] produces "9"</para>
917+
/// <para>The range [-4, -1] produces "789"</para>
918+
/// <para>The range [-4, null] produces "7890"</para>
919+
/// <para>The range [-4, null] produces "7890"</para>
920+
/// <para>The range [-9, -3] produces "234567"</para>
921+
/// <para>
922+
/// This indexer does not raise exceptions for invalid indexes, instead the value
923+
/// returned is the ustring.Empty value:
924+
/// </para>
925+
/// <para>
926+
/// The range [100, 200] produces the ustring.Empty
927+
/// </para>
928+
/// <para>
929+
/// The range [-100, 0] produces ustring.Empty
930+
/// </para>
931+
/// <para>
932+
/// To simulate the optional end boundary, use the indexer that takes the
933+
/// object parameter and pass a null to it. For example, to fetch all
934+
/// elements from the position five until the end, use [5, null]
935+
/// </para>
936+
/// </remarks>
937+
public ustring this [int start, object end] {
938+
get {
939+
int size = Length;
940+
int iend = size;
941+
if (start < 0)
942+
start = size + start;
943+
944+
if (start < 0 || start >= size || start >= iend)
945+
return Empty;
946+
if (iend < 0 || iend > size)
947+
return Empty;
948+
return GetRange (start, iend);
949+
}
950+
}
951+
898952
/// <summary>
899953
/// Returns the substring starting at the given position.
900954
/// </summary>
@@ -1476,7 +1530,7 @@ ustring [] GenSplit (ustring sep, int sepSave, int n = -1)
14761530
offset = m + sepLen;
14771531
i++;
14781532
}
1479-
result [i] = this [offset, 0];
1533+
result [i] = this [offset, null];
14801534
return result;
14811535
}
14821536

@@ -1857,7 +1911,7 @@ public ustring TrimStart (RunePredicate predicate)
18571911
var i = FlexIndexOf (predicate, false);
18581912
if (i == -1)
18591913
return this;
1860-
return this [i, 0];
1914+
return this [i, null];
18611915
}
18621916

18631917
RunePredicate MakeCutSet (ustring cutset)
@@ -1913,7 +1967,7 @@ public ustring TrimEnd (RunePredicate predicate)
19131967
{
19141968
var i = FlexLastIndexOf (predicate, false);
19151969
if (i >= 0 && this [i] >= Utf8.RuneSelf) {
1916-
(var rune, var wid) = Utf8.DecodeRune (this [i, 0]);
1970+
(var rune, var wid) = Utf8.DecodeRune (this [i, null]);
19171971
i += wid;
19181972
} else
19191973
i++;

NStackTests/Utf8Test.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public void TestSequence (ustring s)
203203
j++;
204204
(var r1, var size1) = Utf8.DecodeRune (Subset (s.ToByteArray (), i, -1));
205205
Assert.AreEqual (rune, r1, "DecodeRune 0x{0:x} = want 0x{1:x} with {2}", r1, rune, s);
206-
(var r2, var size2) = Utf8.DecodeRune (s [i, 0]);
206+
(var r2, var size2) = Utf8.DecodeRune (s [i, null]);
207207
Assert.AreEqual (size1, size2);
208208
si += size1;
209209
}

NStackTests/ustringTest.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ public void Equals ()
115115
Assert.IsTrue (ast [1, 5] == bp [1, 5]);
116116
Assert.IsTrue (ap [1, 5] == bst [1, 5]);
117117
Assert.IsTrue (ast [1, 5] == bp [1, 5]);
118-
Assert.IsTrue (ast [8, 0] != cp [8, 0]);
118+
Assert.IsTrue (ast [8, null] != cp [8, null]);
119119
Assert.IsTrue (cp [1, 5] == cpalias [1, 5]);
120120
Assert.IsTrue (cp [1, 5] == cst [1, 5]);
121-
Assert.IsTrue (cp [8, 0] != bst [8, 0]);
121+
Assert.IsTrue (cp [8, null] != bst [8, null]);
122122

123123
}
124124

@@ -324,15 +324,18 @@ void SliceTests (ustring a)
324324
{
325325
Assert.AreEqual ("1234", a [0, 4].ToString ());
326326
Assert.AreEqual ("90", a [8, 10].ToString ());
327-
Assert.AreEqual ("90", a [8, 0].ToString ());
328-
Assert.AreEqual ("90", a [-2, 0].ToString ());
327+
Assert.AreEqual ("90", a [8, null].ToString ());
328+
Assert.AreEqual ("90", a [-2, null].ToString ());
329329
Assert.AreEqual ("9", a [8, 9].ToString ());
330330
Assert.AreEqual ("789", a [-4, -1].ToString ());
331-
Assert.AreEqual ("7890", a [-4, 0].ToString ());
332-
Assert.AreEqual ("7890", a [-4, 0].ToString ());
331+
Assert.AreEqual ("7890", a [-4, null].ToString ());
332+
Assert.AreEqual ("7890", a [-4, null].ToString ());
333333
Assert.AreEqual ("234567", a [-9, -3].ToString ());
334334
Assert.AreEqual ("", a [100, 200].ToString ());
335+
Assert.AreEqual ("", a [-100, null].ToString ());
336+
335337
Assert.AreEqual ("", a [-100, 0].ToString ());
338+
Assert.AreEqual ("", a [0, 0].ToString ());
336339
}
337340

338341
[Test]
@@ -396,6 +399,7 @@ public void Split ()
396399
{
397400
var gecos = ustring.Make ("miguel:*:100:200:Miguel de Icaza:/home/miguel:/bin/bash");
398401
var fields = gecos.Split (":");
402+
399403
Assert.AreEqual (7, fields.Length);
400404
Assert.IsTrue (ustring.Make ("miguel") == fields [0]);
401405
Assert.IsTrue (ustring.Make ("*") == fields [1]);

0 commit comments

Comments
 (0)