Exemple #1
0
        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));
        }
Exemple #2
0
        public static string ReDecodeConvertedStringAsUtf8(StringSlice decoded, Encoding encoding)
        {
            if (Encoding.UTF8.Equals(encoding))
            {
                return(decoded.ToString());
            }
            if (decoded.IsFullString)
            {
                return(ReDecodeConvertedStringAsUtf8(decoded.Text, encoding));
            }

            var encodedBytes     = new byte[encoding.GetMaxByteCount(decoded.Length)];
            var byteEncodedCount = encoding.GetBytes(decoded.Text, decoded.Offset, decoded.Length, encodedBytes, 0);

            return(Encoding.UTF8.GetString(encodedBytes, 0, byteEncodedCount));
        }
        public static Encoding GetEncodingByName(StringSlice encodingName)
        {
            if (encodingName.IsEmpty)
            {
                return(null);
            }

            if (encodingName.Equals("UTF-8", StringComparison.OrdinalIgnoreCase) || encodingName.Equals("UTF8", StringComparison.OrdinalIgnoreCase))
            {
                return(Encoding.UTF8);
            }

            var encodingNameString = encodingName.ToString();

            try
            {
                return(Encoding.GetEncoding(encodingNameString));
            }
            catch (ArgumentException)
            {
                return(GetEncodingByAlternateNames(encodingNameString));
            }
        }
Exemple #4
0
 public static bool TryParseInvariant(StringSlice text, out int value) =>
 int.TryParse(text.ToString(), NumberStyles.Integer, InvariantNumberFormat, out value);