public static string MakeInitCap(StringSlice s, TextInfo textInfo) { #if DEBUG if (textInfo == null) { throw new ArgumentNullException(nameof(textInfo)); } #endif if (s.Length == 0) { return(string.Empty); } var actualFirstLetter = s.First(); var expectedFirstLetter = textInfo.ToUpper(actualFirstLetter); if (expectedFirstLetter == actualFirstLetter) { return(s.ToString()); } if (s.Length == 1) { return(expectedFirstLetter.ToString()); } var builder = StringBuilderPool.Get(s); builder[0] = expectedFirstLetter; return(StringBuilderPool.GetStringAndReturn(builder)); }
public static CapitalizationType GetCapitalizationType(StringSlice word, TextInfo textInfo) { if (word.IsEmpty) { return(CapitalizationType.None); } var hasFoundMoreCaps = false; var firstIsUpper = false; var hasLower = false; var c = word.First(); if (char.IsUpper(c)) { firstIsUpper = true; } else if (CharIsNotNeutral(c, textInfo)) { hasLower = true; } var wordIndexLimit = word.Length + word.Offset; for (int i = word.Offset + 1; i < wordIndexLimit; i++) { c = word.Text[i]; if (!hasFoundMoreCaps && char.IsUpper(c)) { hasFoundMoreCaps = true; if (hasLower) { break; } } else if (!hasLower && CharIsNotNeutral(c, textInfo)) { hasLower = true; if (hasFoundMoreCaps) { break; } } } if (firstIsUpper) { if (!hasFoundMoreCaps) { return(CapitalizationType.Init); } if (!hasLower) { return(CapitalizationType.All); } return(CapitalizationType.HuhInit); } else { if (!hasFoundMoreCaps) { return(CapitalizationType.None); } if (!hasLower) { return(CapitalizationType.All); } return(CapitalizationType.Huh); } }