Fixes ToCamelCase bug #828

This commit is contained in:
Tydude4Christ
2016-03-01 13:36:36 -06:00
parent 34da77c3bf
commit d9ff3ee160
2 changed files with 12 additions and 12 deletions
@@ -55,6 +55,7 @@ namespace Newtonsoft.Json.Tests.Utilities
Assert.AreEqual("iPhone", StringUtils.ToCamelCase("IPhone"));
Assert.AreEqual("i Phone", StringUtils.ToCamelCase("I Phone"));
Assert.AreEqual(" IPhone", StringUtils.ToCamelCase(" IPhone"));
Assert.AreEqual("isCIA", StringUtils.ToCamelCase("IsCIA"));
}
}
}
+11 -12
View File
@@ -170,26 +170,25 @@ namespace Newtonsoft.Json.Utilities
public static string ToCamelCase(string s)
{
if (string.IsNullOrEmpty(s))
{
return s;
}
if (!char.IsUpper(s[0]))
if (string.IsNullOrEmpty(s) || !char.IsUpper(s[0]))
{
return s;
}
char[] chars = s.ToCharArray();
for (int i = 0; i < chars.Length; i++)
int toLowerUntilIndex = 1;
for(; toLowerUntilIndex < chars.Length && char.IsUpper(chars[toLowerUntilIndex]); ++toLowerUntilIndex)
{
bool hasNext = (i + 1 < chars.Length);
if (i > 0 && hasNext && !char.IsUpper(chars[i + 1]))
{
break;
}
}
// decrement if starts with consecutive upper case letters but is not all upper
if (toLowerUntilIndex > 1 && toLowerUntilIndex < chars.Length)
{
--toLowerUntilIndex;
}
for (int i = 0; i < toLowerUntilIndex; ++i)
{
#if !(DOTNET || PORTABLE)
chars[i] = char.ToLower(chars[i], CultureInfo.InvariantCulture);
#else