diff options
Diffstat (limited to 'src/mscorlib/src/System/String.Manipulation.cs')
-rw-r--r-- | src/mscorlib/src/System/String.Manipulation.cs | 247 |
1 files changed, 143 insertions, 104 deletions
diff --git a/src/mscorlib/src/System/String.Manipulation.cs b/src/mscorlib/src/System/String.Manipulation.cs index 194b4f8c59..e06141d669 100644 --- a/src/mscorlib/src/System/String.Manipulation.cs +++ b/src/mscorlib/src/System/String.Manipulation.cs @@ -14,10 +14,6 @@ namespace System { public partial class String { - private const int TrimHead = 0; - private const int TrimTail = 1; - private const int TrimBoth = 2; - unsafe private static void FillStringChecked(String dest, int destPos, String src) { Contract.Requires(dest != null); @@ -176,7 +172,6 @@ namespace System return result; } - [ComVisible(false)] public static string Concat<T>(IEnumerable<T> values) { if (values == null) @@ -227,7 +222,6 @@ namespace System } - [ComVisible(false)] public static string Concat(IEnumerable<string> values) { if (values == null) @@ -575,7 +569,6 @@ namespace System return Join(separator, value, 0, value.Length); } - [ComVisible(false)] public unsafe static string Join(string separator, params object[] values) { separator = separator ?? string.Empty; @@ -586,7 +579,6 @@ namespace System } } - [ComVisible(false)] public unsafe static string Join<T>(string separator, IEnumerable<T> values) { separator = separator ?? string.Empty; @@ -597,7 +589,6 @@ namespace System } } - [ComVisible(false)] public static string Join(string separator, IEnumerable<string> values) { if (values == null) @@ -1043,16 +1034,14 @@ namespace System return ReplaceInternal(oldValue, newValue); } - [ComVisible(false)] - public String[] Split(char separator, StringSplitOptions options = StringSplitOptions.None) { + public unsafe String[] Split(char separator, StringSplitOptions options = StringSplitOptions.None) { Contract.Ensures(Contract.Result<String[]>() != null); - return SplitInternal(separator, Int32.MaxValue, options); + return SplitInternal(&separator, 1, int.MaxValue, options); } - [ComVisible(false)] - public String[] Split(char separator, int count, StringSplitOptions options = StringSplitOptions.None) { + public unsafe String[] Split(char separator, int count, StringSplitOptions options = StringSplitOptions.None) { Contract.Ensures(Contract.Result<String[]>() != null); - return SplitInternal(separator, count, options); + return SplitInternal(&separator, 1, count, options); } // Creates an array of strings by splitting this string at each @@ -1085,24 +1074,17 @@ namespace System return SplitInternal(separator, count, StringSplitOptions.None); } - [ComVisible(false)] public String[] Split(char[] separator, StringSplitOptions options) { Contract.Ensures(Contract.Result<String[]>() != null); return SplitInternal(separator, Int32.MaxValue, options); } - [ComVisible(false)] public String[] Split(char[] separator, int count, StringSplitOptions options) { Contract.Ensures(Contract.Result<String[]>() != null); return SplitInternal(separator, count, options); } - private unsafe String[] SplitInternal(char separator, int count, StringSplitOptions options) - { - return SplitInternal(&separator, 1, count, options); - } - private unsafe String[] SplitInternal(char[] separator, int count, StringSplitOptions options) { fixed (char* pSeparators = separator) @@ -1153,25 +1135,21 @@ namespace System } } - [ComVisible(false)] public String[] Split(String separator, StringSplitOptions options = StringSplitOptions.None) { Contract.Ensures(Contract.Result<String[]>() != null); return SplitInternal(separator ?? String.Empty, null, Int32.MaxValue, options); } - [ComVisible(false)] public String[] Split(String separator, Int32 count, StringSplitOptions options = StringSplitOptions.None) { Contract.Ensures(Contract.Result<String[]>() != null); return SplitInternal(separator ?? String.Empty, null, count, options); } - [ComVisible(false)] public String [] Split(String[] separator, StringSplitOptions options) { Contract.Ensures(Contract.Result<String[]>() != null); return SplitInternal(null, separator, Int32.MaxValue, options); } - [ComVisible(false)] public String[] Split(String[] separator, Int32 count, StringSplitOptions options) { Contract.Ensures(Contract.Result<String[]>() != null); return SplitInternal(null, separator, count, options); @@ -1536,116 +1514,177 @@ namespace System Contract.EndContractBlock(); return this.ToUpper(CultureInfo.InvariantCulture); } - - // Removes a set of characters from the end of this string. + + // Trims the whitespace from both ends of the string. Whitespace is defined by + // Char.IsWhiteSpace. + // [Pure] - public String Trim(params char[] trimChars) { - if (null==trimChars || trimChars.Length == 0) { - return TrimHelper(TrimBoth); + public string Trim() + { + Contract.Ensures(Contract.Result<string>() != null); + Contract.EndContractBlock(); + + return TrimWhiteSpaceHelper(TrimType.Both); + } + + // Removes a set of characters from the beginning and end of this string. + public unsafe string Trim(char trimChar) => TrimHelper(&trimChar, 1, TrimType.Both); + + // Removes a set of characters from the beginning and end of this string. + [Pure] + public unsafe string Trim(params char[] trimChars) + { + if (trimChars == null || trimChars.Length == 0) + { + return TrimWhiteSpaceHelper(TrimType.Both); + } + fixed (char* pTrimChars = &trimChars[0]) + { + return TrimHelper(pTrimChars, trimChars.Length, TrimType.Both); } - return TrimHelper(trimChars,TrimBoth); } - + + // Removes a set of characters from the beginning of this string. + public string TrimStart() => TrimWhiteSpaceHelper(TrimType.Head); + // Removes a set of characters from the beginning of this string. - public String TrimStart(params char[] trimChars) { - if (null==trimChars || trimChars.Length == 0) { - return TrimHelper(TrimHead); + public unsafe string TrimStart(char trimChar) => TrimHelper(&trimChar, 1, TrimType.Head); + + // Removes a set of characters from the beginning of this string. + public unsafe string TrimStart(params char[] trimChars) + { + if (trimChars == null || trimChars.Length == 0) + { + return TrimWhiteSpaceHelper(TrimType.Head); } - return TrimHelper(trimChars,TrimHead); - } - - - // Removes a set of characters from the end of this string. - public String TrimEnd(params char[] trimChars) { - if (null==trimChars || trimChars.Length == 0) { - return TrimHelper(TrimTail); + fixed (char* pTrimChars = &trimChars[0]) + { + return TrimHelper(pTrimChars, trimChars.Length, TrimType.Head); } - return TrimHelper(trimChars,TrimTail); } - // Trims the whitespace from both ends of the string. Whitespace is defined by - // Char.IsWhiteSpace. - // - [Pure] - public String Trim() { - Contract.Ensures(Contract.Result<String>() != null); - Contract.EndContractBlock(); + // Removes a set of characters from the end of this string. + public string TrimEnd() => TrimWhiteSpaceHelper(TrimType.Tail); - return TrimHelper(TrimBoth); + // Removes a set of characters from the end of this string. + public unsafe string TrimEnd(char trimChar) => TrimHelper(&trimChar, 1, TrimType.Tail); + + // Removes a set of characters from the end of this string. + public unsafe string TrimEnd(params char[] trimChars) + { + if (trimChars == null || trimChars.Length == 0) + { + return TrimWhiteSpaceHelper(TrimType.Tail); + } + fixed (char* pTrimChars = &trimChars[0]) + { + return TrimHelper(pTrimChars, trimChars.Length, TrimType.Tail); + } } - - private String TrimHelper(int trimType) { - //end will point to the first non-trimmed character on the right - //start will point to the first non-trimmed character on the Left - int end = this.Length-1; - int start=0; - - //Trim specified characters. - if (trimType !=TrimTail) { - for (start=0; start < this.Length; start++) { - if (!Char.IsWhiteSpace(this[start])) break; + private string TrimWhiteSpaceHelper(TrimType trimType) + { + // end will point to the first non-trimmed character on the right. + // start will point to the first non-trimmed character on the left. + int end = Length - 1; + int start = 0; + + // Trim specified characters. + if (trimType != TrimType.Tail) + { + for (start = 0; start < Length; start++) + { + if (!char.IsWhiteSpace(this[start])) + { + break; + } } } - - if (trimType !=TrimHead) { - for (end= Length -1; end >= start; end--) { - if (!Char.IsWhiteSpace(this[end])) break; + + if (trimType != TrimType.Head) + { + for (end = Length - 1; end >= start; end--) + { + if (!char.IsWhiteSpace(this[end])) + { + break; + } } } return CreateTrimmedString(start, end); } - - - private String TrimHelper(char[] trimChars, int trimType) { - //end will point to the first non-trimmed character on the right - //start will point to the first non-trimmed character on the Left - int end = this.Length-1; - int start=0; - - //Trim specified characters. - if (trimType !=TrimTail) { - for (start=0; start < this.Length; start++) { + + private unsafe string TrimHelper(char* trimChars, int trimCharsLength, TrimType trimType) + { + Debug.Assert(trimChars != null); + Debug.Assert(trimCharsLength > 0); + + // end will point to the first non-trimmed character on the right. + // start will point to the first non-trimmed character on the left. + int end = Length - 1; + int start = 0; + + // Trim specified characters. + if (trimType != TrimType.Tail) + { + for (start = 0; start < Length; start++) + { int i = 0; char ch = this[start]; - for( i = 0; i < trimChars.Length; i++) { - if( trimChars[i] == ch) break; + for (i = 0; i < trimCharsLength; i++) + { + if (trimChars[i] == ch) + { + break; + } } - if( i == trimChars.Length) { // the character is not white space - break; + if (i == trimCharsLength) + { + // The character is not in trimChars, so stop trimming. + break; } } } - - if (trimType !=TrimHead) { - for (end= Length -1; end >= start; end--) { - int i = 0; - char ch = this[end]; - for(i = 0; i < trimChars.Length; i++) { - if( trimChars[i] == ch) break; + + if (trimType != TrimType.Head) + { + for (end = Length - 1; end >= start; end--) + { + int i = 0; + char ch = this[end]; + for (i = 0; i < trimCharsLength; i++) + { + if (trimChars[i] == ch) + { + break; + } + } + if (i == trimCharsLength) + { + // The character is not in trimChars, so stop trimming. + break; } - if( i == trimChars.Length) { // the character is not white space - break; - } } } return CreateTrimmedString(start, end); } + private string CreateTrimmedString(int start, int end) + { + int len = end - start + 1; + return + len == Length ? this : + len == 0 ? string.Empty : + InternalSubString(start, len); + } - private String CreateTrimmedString(int start, int end) { - int len = end -start + 1; - if (len == this.Length) { - // Don't allocate a new string as the trimmed string has not changed. - return this; - } - - if( len == 0) { - return String.Empty; - } - return InternalSubString(start, len); + private enum TrimType + { + Head = 0, + Tail = 1, + Both = 2 } } } |