Skip to content

Commit

Permalink
fix: camel to camel and snake to snake
Browse files Browse the repository at this point in the history
  • Loading branch information
ExerciseBook committed Jun 8, 2024
1 parent fd2d850 commit ee5f3a4
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 14 deletions.
64 changes: 58 additions & 6 deletions OneBot-Interface/Core/Util/StringUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,34 @@ public enum CamelCaseType
{
Upper,

Lower
Lower,

None,
}

public enum CaseType
{
Lower,

Upper
Upper,

None,
}

private static CaseType EvaluateCaseType(char c)
{
if (char.IsUpper(c))
{
return CaseType.Upper;
}

if (char.IsLower(c))
{
return CaseType.Lower;
}

return CaseType.None;
}

public static bool IsNullOrEmpty(string? value)
=> string.IsNullOrEmpty(value);
Expand Down Expand Up @@ -80,9 +98,14 @@ public static string ToCamelCase(string s, CamelCaseType caseType, string[]? loc
char[] result = new char[src.Length - ulCnt];
bool fstWord = true, fstLetter = true;
string tmp = "";

for (int i = 0, j = 0; i <= src.Length; i++)
{
if (i == src.Length || src[i] == Underline || src[i] == Dash || char.IsSeparator(src[i]))
if (i == src.Length ||
src[i] == Underline || src[i] == Dash ||
char.IsSeparator(src[i]) ||
(i > 0 && char.IsUpper(src[i]))
)
{
if (lockedWords != null && lockedWords.Contains(tmp))
{
Expand All @@ -95,13 +118,21 @@ public static string ToCamelCase(string s, CamelCaseType caseType, string[]? loc
result[j - tmp.Length + k] = char.ToLower(result[j - tmp.Length + k]);
}
}

if (i < src.Length)
{
if (src[i] == Underline || src[i] == Dash)
{
fstLetter = true;
tmp = "";
}
else if (i > 0 && char.IsUpper(src[i]))
{
fstLetter = false;
result[j] = src[i];
tmp = "" + src[i];
j++;
}
else
{
fstWord = true;
Expand All @@ -128,6 +159,7 @@ public static string ToCamelCase(string s, CamelCaseType caseType, string[]? loc
{
result[j] = char.ToLower(src[i]);
}

tmp += char.ToUpper(src[i]);
j++;
}
Expand Down Expand Up @@ -164,20 +196,31 @@ public static string ToUpperSnakeCase(string s, string[]? lockedWords = null)
/// <returns></returns>
public static string ToSeparatedCase(string s, char separator, CaseType caseType, string[]? lockedWords = null)
{
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder(16);
char[] src = s.ToCharArray();

int t = 0;
var separated = false;

for (int i = 0; i < src.Length; i++, t++)
{

if (char.IsSeparator(src[i]))
{
sb.Append(src[i]);
t = -1;
continue;
}
if (char.IsUpper(src[i]))

if (src[i] == Dash || src[i] == Underline)
{
separated = true;
continue;
}

if (separated || char.IsUpper(src[i]))
{
separated = false;
if (lockedWords != null)
{
bool skip = false;
Expand All @@ -192,6 +235,7 @@ public static string ToSeparatedCase(string s, char separator, CaseType caseType
break;
}
}

if (match)
{
if (t != 0)
Expand All @@ -204,21 +248,29 @@ public static string ToSeparatedCase(string s, char separator, CaseType caseType
sb.Append(char.ToLower(src[i]));
i++;
}

i--;
skip = true;
break;
}
}

if (skip)
continue;
}

if (t != 0)
sb.Append(separator);
}

if (caseType == CaseType.Upper)
{
sb.Append(char.ToUpper(src[i]));
}
else
{
sb.Append(char.ToLower(src[i]));
}
}

return sb.ToString();
Expand Down
27 changes: 19 additions & 8 deletions OneBot-UnitTest/UnitTest/Util/StringUtilsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,33 @@ public void ToSnakeCaseTest()

Assert.AreEqual("one_another", StringUtils.ToSnakeCase("oneAnother", StringUtils.CaseType.Lower, lockedWords));
Assert.AreEqual("ONE_ANOTHER", StringUtils.ToSnakeCase("oneAnother", StringUtils.CaseType.Upper, lockedWords));

Assert.AreEqual("ONE_ANOTHER", StringUtils.ToSnakeCase("one-Another", StringUtils.CaseType.Upper, lockedWords));
Assert.AreEqual("ONE_ANOTHER", StringUtils.ToSnakeCase("one_Another", StringUtils.CaseType.Upper, lockedWords));
Assert.AreEqual("ONE_ANOTHER", StringUtils.ToSnakeCase("one_-Another", StringUtils.CaseType.Upper, lockedWords));
}

[TestMethod]
public void ToLowerCamelTest()
{
Assert.AreEqual("userQQ", StringUtils.ToCamelCase("user_qq", StringUtils.CamelCaseType.Lower, lockedWords));
Assert.AreEqual("userQQ", StringUtils.ToCamelCase("userQQ", StringUtils.CamelCaseType.Lower, lockedWords));
Assert.AreEqual("userQQ", StringUtils.ToCamelCase("userQq", StringUtils.CamelCaseType.Lower, lockedWords));
Assert.AreEqual("userID", StringUtils.ToCamelCase("user_id", StringUtils.CamelCaseType.Lower, lockedWords));
Assert.AreEqual("qqID", StringUtils.ToCamelCase("qq_id", StringUtils.CamelCaseType.Lower, lockedWords));
Assert.AreEqual("icelolly", StringUtils.ToCamelCase("icelolly", StringUtils.CamelCaseType.Lower));
Assert.AreEqual("icelolly", StringUtils.ToCamelCase("Icelolly", StringUtils.CamelCaseType.Lower));
Assert.AreEqual("icelolly", StringUtils.ToCamelCase("IceLOlly", StringUtils.CamelCaseType.Lower));
Assert.AreEqual("iceLOlly", StringUtils.ToCamelCase("IceLOlly", StringUtils.CamelCaseType.Lower));
Assert.AreEqual("icelollyDress", StringUtils.ToCamelCase("icelolly_dress", StringUtils.CamelCaseType.Lower));
Assert.AreEqual("icelollyDress2", StringUtils.ToCamelCase("icelolly_dress2", StringUtils.CamelCaseType.Lower));
Assert.AreEqual("icelollyDress", StringUtils.ToCamelCase("ICelolly_DRess", StringUtils.CamelCaseType.Lower));
Assert.AreEqual("icelollyDress daiSuki", StringUtils.ToCamelCase("iCelolly_dRess dai_suki", StringUtils.CamelCaseType.Lower));
Assert.AreEqual("icelollyDress daiSuki404", StringUtils.ToCamelCase("iCelolly_dRess dai_suki_404", StringUtils.CamelCaseType.Lower));
Assert.AreEqual("icelollyDress2", StringUtils.ToCamelCase("icelolly_dress_2", StringUtils.CamelCaseType.Lower));

// ambiguous case, not supports
Assert.AreEqual("iCelollyDRess", StringUtils.ToCamelCase("ICelolly_DRess", StringUtils.CamelCaseType.Lower));

Assert.AreEqual("iCelollyDRess daiSuki", StringUtils.ToCamelCase("iCelolly_dRess dai_suki", StringUtils.CamelCaseType.Lower));
Assert.AreEqual("iCelollyDRess daiSuki404", StringUtils.ToCamelCase("iCelolly_dRess dai_suki_404", StringUtils.CamelCaseType.Lower));
Assert.AreEqual("iCelollyDRess", StringUtils.ToCamelCase("iCelolly_----dRess", StringUtils.CamelCaseType.Lower));
}

[TestMethod]
Expand All @@ -65,11 +76,11 @@ public void ToUpperCamelTest()
Assert.AreEqual("QQID", StringUtils.ToCamelCase("qq_id", StringUtils.CamelCaseType.Upper, lockedWords));
Assert.AreEqual("Icelolly", StringUtils.ToCamelCase("icelolly", StringUtils.CamelCaseType.Upper));
Assert.AreEqual("Icelolly", StringUtils.ToCamelCase("Icelolly", StringUtils.CamelCaseType.Upper));
Assert.AreEqual("Icelolly", StringUtils.ToCamelCase("IceLOlly", StringUtils.CamelCaseType.Upper));
Assert.AreEqual("IceLOlly", StringUtils.ToCamelCase("IceLOlly", StringUtils.CamelCaseType.Upper));
Assert.AreEqual("IcelollyDress", StringUtils.ToCamelCase("icelolly_dress", StringUtils.CamelCaseType.Upper));
Assert.AreEqual("IcelollyDress2", StringUtils.ToCamelCase("icelolly_dress2", StringUtils.CamelCaseType.Upper));
Assert.AreEqual("IcelollyDress", StringUtils.ToCamelCase("ICelolly_DRess", StringUtils.CamelCaseType.Upper));
Assert.AreEqual("IcelollyDress DaiSuki", StringUtils.ToCamelCase("iCelolly_dRess dai_suki", StringUtils.CamelCaseType.Upper));
Assert.AreEqual("IcelollyDress DaiSuki404", StringUtils.ToCamelCase("iCelolly_dRess dai_suki_404", StringUtils.CamelCaseType.Upper));
Assert.AreEqual("ICelollyDRess", StringUtils.ToCamelCase("ICelolly_DRess", StringUtils.CamelCaseType.Upper));
Assert.AreEqual("ICelollyDRess DaiSuki", StringUtils.ToCamelCase("iCelolly_dRess dai_suki", StringUtils.CamelCaseType.Upper));
Assert.AreEqual("ICelollyDRess DaiSuki404", StringUtils.ToCamelCase("iCelolly_dRess dai_suki_404", StringUtils.CamelCaseType.Upper));
}
}

0 comments on commit ee5f3a4

Please sign in to comment.