public unsafe static int StringToInt(string value, int fromBase, int flags, int *parsePos) { if ((flags & (IsTight | NoSpace)) == 0) { throw new NotImplementedException(flags.ToString()); } if (value == null) { return(0); } int chars = 0; uint result = 0; int digitValue; int len = value.Length; bool negative = false; if (len == 0) { // Mimic broken .net behaviour throw new ArgumentOutOfRangeException("Empty string"); } int i = parsePos == null ? 0 : *parsePos; //Check for a sign if (value [i] == '-') { if (fromBase != 10) { throw new ArgumentException("String cannot contain a minus sign if the base is not 10."); } if ((flags & TreatAsUnsigned) != 0) { throw new OverflowException("Negative number"); } negative = true; i++; } else if (value [i] == '+') { i++; } if (fromBase == 16 && i + 1 < len && value [i] == '0' && (value [i + 1] == 'x' || value [i + 1] == 'X')) { i += 2; } uint max_value; if ((flags & TreatAsI1) != 0) { max_value = Byte.MaxValue; } else if ((flags & TreatAsI2) != 0) { max_value = UInt16.MaxValue; } else { max_value = UInt32.MaxValue; } while (i < len) { char c = value [i]; if (Char.IsNumber(c)) { digitValue = c - '0'; } else if (Char.IsLetter(c)) { digitValue = Char.ToLowerInvariant(c) - 'a' + 10; } else { if (i == 0) { throw new FormatException("Could not find any parsable digits."); } if ((flags & IsTight) != 0) { throw new FormatException("Additional unparsable characters are at the end of the string."); } break; } if (digitValue >= fromBase) { if (chars > 0) { throw new FormatException("Additional unparsable characters are at the end of the string."); } throw new FormatException("Could not find any parsable digits."); } var res = (uint)fromBase * result + (uint)digitValue; if (res < result || res > max_value) { throw new OverflowException(); } result = res; chars++; ++i; } if (chars == 0) { throw new FormatException("Could not find any parsable digits."); } if (parsePos != null) { *parsePos = i; } return(negative ? -(int)result : (int)result); }
public static char ToUpperInvariant(this char c) { return(Char.ToLowerInvariant(c)); }
public unsafe static long StringToLong(string value, int fromBase, int flags, int *parsePos) { if ((flags & (IsTight | NoSpace)) == 0) { throw new NotImplementedException(flags.ToString()); } if (value == null) { return(0); } int chars = 0; ulong fromBaseULong = (ulong)fromBase; ulong digitValue = 0; ulong result = 0; int len = value.Length; bool negative = false; bool treatAsUnsigned = (flags & ParseNumbers.TreatAsUnsigned) != 0; if (len == 0) { // Mimic broken .net behaviour throw new ArgumentOutOfRangeException("Empty string"); } int i = parsePos == null ? 0 : *parsePos; //Check for a sign if (value [i] == '-') { if (fromBase != 10) { throw new ArgumentException("String cannot contain a minus sign if the base is not 10."); } if (treatAsUnsigned) { throw new OverflowException("Negative number"); } negative = true; i++; } else if (value [i] == '+') { i++; } if (fromBase == 16 && i + 1 < len && value [i] == '0' && (value [i + 1] == 'x' || value [i + 1] == 'X')) { i += 2; } while (i < len) { char c = value[i]; if (Char.IsNumber(c)) { digitValue = (ulong)(c - '0'); } else if (Char.IsLetter(c)) { digitValue = (ulong)(Char.ToLowerInvariant(c) - 'a' + 10); } else { if (i == 0) { throw new FormatException("Could not find any parsable digits."); } if ((flags & IsTight) != 0) { throw new FormatException("Additional unparsable characters are at the end of the string."); } break; } if (digitValue >= fromBaseULong) { if (chars > 0) { throw new FormatException("Additional unparsable " + "characters are at the end of the string."); } else { throw new FormatException("Could not find any parsable" + " digits."); } } if (result <= base16MaxOverflowFreeValue) { result = result * (ulong)fromBaseULong + digitValue; } else { // decompose 64 bit operation into 32 bit operations so we can check for overflows ulong a = (result >> 32) * fromBaseULong; ulong b = (result & uint.MaxValue) * fromBaseULong + digitValue; if (((b >> 32) + a) > uint.MaxValue) { throw new OverflowException(); } result = (a << 32) + b; } chars++; ++i; } if (chars == 0) { throw new FormatException("Could not find any parsable digits."); } if (parsePos != null) { *parsePos = i; } if (treatAsUnsigned) { return((long)result); } if (!negative) { if (fromBase == 10 && result > ((ulong)long.MaxValue)) { throw new OverflowException(); } return((long)result); } if (result <= (ulong)long.MaxValue) { return(-((long)result)); } if (result > longMinValue) { throw new OverflowException(); } // Avoids overflow of -result when result > long.MaxValue return(long.MinValue + (long)(longMinValue - result)); }
/// <summary> /// Converts the specified string to camel case (except for words that are entirely in uppercase, which are considered to be acronyms). /// </summary> /// <param name="String">The string to convert to camel case.</param> /// <returns>The specified string converted to camel case</returns> public static String ToCamelCase(this String String) => Char.ToLowerInvariant(String[0]) + String.ToPascalCase().Substring(1);
public static string LowerFirstChar(this string name) { return(Char.ToLowerInvariant(name[0]) + name.Substring(1)); }
public static Char ToLowerInvariant(this Char c) { return(Char.ToLowerInvariant(c)); }
/// <summary> /// Converts the value of a Unicode character to its lowercase equivalent using the casing rules of the invariant culture. /// </summary> /// <param name="char">The Unicode character to convert.</param> /// <returns>The lowercase equivalent of the <paramref name="char"/> parameter, or the unchanged value of <paramref name="char"/>, if <paramref name="char"/> is already lowercase or not alphabetic.</returns> public static Char ToLowerInvariant(this Char @char) => Char.ToLowerInvariant(@char);
public static long StringToLong(string value, int fromBase, int flags) { if ((flags & IsTight) == 0) { throw new NotImplementedException(); } if (value == null) { return(0); } int chars = 0; int digitValue = -1; long result = 0; int i = 0; int len = value.Length; bool negative = false; if (len == 0) { // Mimic broken .net behaviour throw new ArgumentOutOfRangeException("Empty string"); } //Check for a sign if (value [i] == '-') { if (fromBase != 10) { throw new ArgumentException("String cannot contain a minus sign if the base is not 10."); } if ((flags & TreatAsUnsigned) != 0) { throw new OverflowException("Negative number"); } negative = true; i++; } else if (value [i] == '+') { i++; } if (fromBase == 16 && i + 1 < len && value [i] == '0' && (value [i + 1] == 'x' || value [i + 1] == 'X')) { i += 2; } while (i < len) { char c = value[i++]; if (Char.IsNumber(c)) { digitValue = c - '0'; } else if (Char.IsLetter(c)) { digitValue = Char.ToLowerInvariant(c) - 'a' + 10; } else { if (chars > 0) { throw new FormatException("Additional unparsable " + "characters are at the end of the string."); } else { throw new FormatException("Could not find any parsable" + " digits."); } } if (digitValue >= fromBase) { if (chars > 0) { throw new FormatException("Additional unparsable " + "characters are at the end of the string."); } else { throw new FormatException("Could not find any parsable" + " digits."); } } result = fromBase * result + digitValue; chars++; } if (chars == 0) { throw new FormatException("Could not find any parsable digits."); } return(negative ? -result : result); }