diff options
Diffstat (limited to 'src/mscorlib/corefx/System/Globalization/StringInfo.cs')
-rw-r--r-- | src/mscorlib/corefx/System/Globalization/StringInfo.cs | 89 |
1 files changed, 72 insertions, 17 deletions
diff --git a/src/mscorlib/corefx/System/Globalization/StringInfo.cs b/src/mscorlib/corefx/System/Globalization/StringInfo.cs index 102f703beb..7558002413 100644 --- a/src/mscorlib/corefx/System/Globalization/StringInfo.cs +++ b/src/mscorlib/corefx/System/Globalization/StringInfo.cs @@ -13,6 +13,7 @@ //////////////////////////////////////////////////////////////////////////// using System; +using System.Diagnostics; using System.Diagnostics.Contracts; using System.Runtime.Serialization; @@ -20,10 +21,10 @@ namespace System.Globalization { [Serializable] [System.Runtime.InteropServices.ComVisible(true)] - public partial class StringInfo + public class StringInfo { [OptionalField(VersionAdded = 2)] - private String _str; + private string _str; [NonSerialized] private int[] _indexes; @@ -32,7 +33,7 @@ namespace System.Globalization public StringInfo() : this("") { } // Primary, useful constructor - public StringInfo(String value) + public StringInfo(string value) { this.String = value; } @@ -85,7 +86,7 @@ namespace System.Globalization } } - public String String + public string String { get { @@ -119,7 +120,61 @@ namespace System.Globalization } } - public static String GetNextTextElement(String str) + public string SubstringByTextElements(int startingTextElement) + { + // If the string is empty, no sense going further. + if (null == this.Indexes) + { + // Just decide which error to give depending on the param they gave us.... + if (startingTextElement < 0) + { + throw new ArgumentOutOfRangeException(nameof(startingTextElement), SR.ArgumentOutOfRange_NeedPosNum); + } + else + { + throw new ArgumentOutOfRangeException(nameof(startingTextElement), SR.Arg_ArgumentOutOfRangeException); + } + } + return (SubstringByTextElements(startingTextElement, Indexes.Length - startingTextElement)); + } + + public string SubstringByTextElements(int startingTextElement, int lengthInTextElements) + { + if (startingTextElement < 0) + { + throw new ArgumentOutOfRangeException(nameof(startingTextElement), SR.ArgumentOutOfRange_NeedPosNum); + } + + if (this.String.Length == 0 || startingTextElement >= Indexes.Length) + { + throw new ArgumentOutOfRangeException(nameof(startingTextElement), SR.Arg_ArgumentOutOfRangeException); + } + + if (lengthInTextElements < 0) + { + throw new ArgumentOutOfRangeException(nameof(lengthInTextElements), SR.ArgumentOutOfRange_NeedPosNum); + } + + if (startingTextElement > Indexes.Length - lengthInTextElements) + { + throw new ArgumentOutOfRangeException(nameof(lengthInTextElements), SR.Arg_ArgumentOutOfRangeException); + } + + int start = Indexes[startingTextElement]; + + if (startingTextElement + lengthInTextElements == Indexes.Length) + { + // We are at the last text element in the string and because of that + // must handle the call differently. + return (this.String.Substring(start)); + } + else + { + return (this.String.Substring(start, (Indexes[lengthInTextElements + startingTextElement] - start))); + } + } + + public static string GetNextTextElement(string str) { return (GetNextTextElement(str, 0)); } @@ -157,10 +212,10 @@ namespace System.Globalization // //////////////////////////////////////////////////////////////////////// - internal static int GetCurrentTextElementLen(String str, int index, int len, ref UnicodeCategory ucCurrent, ref int currentCharCount) + internal static int GetCurrentTextElementLen(string str, int index, int len, ref UnicodeCategory ucCurrent, ref int currentCharCount) { - Contract.Assert(index >= 0 && len >= 0, "StringInfo.GetCurrentTextElementLen() : index = " + index + ", len = " + len); - Contract.Assert(index < len, "StringInfo.GetCurrentTextElementLen() : index = " + index + ", len = " + len); + Debug.Assert(index >= 0 && len >= 0, "StringInfo.GetCurrentTextElementLen() : index = " + index + ", len = " + len); + Debug.Assert(index < len, "StringInfo.GetCurrentTextElementLen() : index = " + index + ", len = " + len); if (index + currentCharCount == len) { // This is the last character/surrogate in the string. @@ -220,14 +275,14 @@ namespace System.Globalization // of str. It recognizes a base character plus one or more combining // characters or a properly formed surrogate pair as a text element. See also // the ParseCombiningCharacters() and the ParseSurrogates() methods. - public static String GetNextTextElement(String str, int index) + public static string GetNextTextElement(string str, int index) { // // Validate parameters. // if (str == null) { - throw new ArgumentNullException("str"); + throw new ArgumentNullException(nameof(str)); } Contract.EndContractBlock(); @@ -238,7 +293,7 @@ namespace System.Globalization { return (String.Empty); } - throw new ArgumentOutOfRangeException("index", SR.ArgumentOutOfRange_Index); + throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_Index); } int charLen; @@ -246,26 +301,26 @@ namespace System.Globalization return (str.Substring(index, GetCurrentTextElementLen(str, index, len, ref uc, ref charLen))); } - public static TextElementEnumerator GetTextElementEnumerator(String str) + public static TextElementEnumerator GetTextElementEnumerator(string str) { return (GetTextElementEnumerator(str, 0)); } - public static TextElementEnumerator GetTextElementEnumerator(String str, int index) + public static TextElementEnumerator GetTextElementEnumerator(string str, int index) { // // Validate parameters. // if (str == null) { - throw new ArgumentNullException("str"); + throw new ArgumentNullException(nameof(str)); } Contract.EndContractBlock(); int len = str.Length; if (index < 0 || (index > len)) { - throw new ArgumentOutOfRangeException("index", SR.ArgumentOutOfRange_Index); + throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_Index); } return (new TextElementEnumerator(str, index, len)); @@ -283,11 +338,11 @@ namespace System.Globalization * return the indices: 0, 2, 4. */ - public static int[] ParseCombiningCharacters(String str) + public static int[] ParseCombiningCharacters(string str) { if (str == null) { - throw new ArgumentNullException("str"); + throw new ArgumentNullException(nameof(str)); } Contract.EndContractBlock(); |