Skip to content

Commit

Permalink
[corlib]String.IndexOf is culture aware, while String.Contains is not.
Browse files Browse the repository at this point in the history
  • Loading branch information
kumpera committed Jun 22, 2013
1 parent 40facc7 commit 9f9ba76
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
9 changes: 6 additions & 3 deletions mcs/class/corlib/System/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1278,11 +1278,11 @@ public int IndexOf (String value)
{
if (value == null)
throw new ArgumentNullException ("value");
if (value.length == 0)
if (value.Length == 0)
return 0;
if (this.length == 0)
return -1;
return CultureInfo.CurrentCulture.CompareInfo.IndexOf (this, value, 0, length, CompareOptions.Ordinal);
return CultureInfo.CurrentCulture.CompareInfo.IndexOf (this, value, 0, length, CompareOptions.None);
}

public int IndexOf (String value, int startIndex)
Expand Down Expand Up @@ -1516,7 +1516,10 @@ public int LastIndexOf (String value, int startIndex, int count)

public bool Contains (String value)
{
return IndexOf (value) != -1;
if (value == null)
throw new ArgumentNullException ("value");

return IndexOfOrdinalUnchecked (value, 0, Length) != -1;
}

public static bool IsNullOrEmpty (String value)
Expand Down
20 changes: 19 additions & 1 deletion mcs/class/corlib/Test/System/StringTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2274,7 +2274,25 @@ public void Contains ()
Assert.IsTrue ("ABC".Contains ("ABC"));
Assert.IsTrue ("ABC".Contains ("AB"));
Assert.IsTrue (!"ABC".Contains ("AD"));
Assert.IsTrue (!"encyclop�dia".Contains("encyclopaedia"));
Assert.IsTrue (!"encyclopædia".Contains("encyclopaedia"));
}

[Test]
public void IndexOfIsCultureAwareWhileContainsIsNot ()
{
string a = "encyclopædia";
string b = "encyclopaedia";
Assert.IsFalse (a.Contains (b), "#1");
Assert.IsTrue (a.Contains ("æ"), "#1.1");
Assert.IsFalse (b.Contains ("æ"), "#1.2");
Assert.AreEqual (0, a.IndexOf (b), "#2");
Assert.AreEqual (8, a.IndexOf ('æ'), "#3");
Assert.AreEqual (-1, b.IndexOf ('æ'), "#4");
Assert.AreEqual (8, a.IndexOf ("æ"), "#5");
Assert.AreEqual (8, b.IndexOf ("æ"), "#6");

Assert.AreEqual (0, CultureInfo.CurrentCulture.CompareInfo.IndexOf (a, b, 0, a.Length, CompareOptions.None), "#7");
Assert.AreEqual (-1, CultureInfo.CurrentCulture.CompareInfo.IndexOf (a, b, 0, a.Length, CompareOptions.Ordinal), "#8");
}

[Test]
Expand Down

0 comments on commit 9f9ba76

Please sign in to comment.