From ee5f3a44906412c8d3582ff241c8789f48c752b2 Mon Sep 17 00:00:00 2001 From: Eric_Lian Date: Sat, 8 Jun 2024 13:34:55 +0800 Subject: [PATCH] fix: camel to camel and snake to snake --- OneBot-Interface/Core/Util/StringUtils.cs | 64 +++++++++++++++++-- .../UnitTest/Util/StringUtilsTest.cs | 27 +++++--- 2 files changed, 77 insertions(+), 14 deletions(-) diff --git a/OneBot-Interface/Core/Util/StringUtils.cs b/OneBot-Interface/Core/Util/StringUtils.cs index bb1cc67..e6a51bc 100644 --- a/OneBot-Interface/Core/Util/StringUtils.cs +++ b/OneBot-Interface/Core/Util/StringUtils.cs @@ -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); @@ -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)) { @@ -95,6 +118,7 @@ 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) @@ -102,6 +126,13 @@ public static string ToCamelCase(string s, CamelCaseType caseType, string[]? loc fstLetter = true; tmp = ""; } + else if (i > 0 && char.IsUpper(src[i])) + { + fstLetter = false; + result[j] = src[i]; + tmp = "" + src[i]; + j++; + } else { fstWord = true; @@ -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++; } @@ -164,20 +196,31 @@ public static string ToUpperSnakeCase(string s, string[]? lockedWords = null) /// 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; @@ -192,6 +235,7 @@ public static string ToSeparatedCase(string s, char separator, CaseType caseType break; } } + if (match) { if (t != 0) @@ -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(); diff --git a/OneBot-UnitTest/UnitTest/Util/StringUtilsTest.cs b/OneBot-UnitTest/UnitTest/Util/StringUtilsTest.cs index ef406c6..3af88fa 100644 --- a/OneBot-UnitTest/UnitTest/Util/StringUtilsTest.cs +++ b/OneBot-UnitTest/UnitTest/Util/StringUtilsTest.cs @@ -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] @@ -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)); } }