private CharacterRange[] AdjustCharacterRangesForSurrogateChars()
 {
     string text = this.Text;
     if (string.IsNullOrEmpty(text))
     {
         return new CharacterRange[0];
     }
     StringInfo info = new StringInfo(text);
     int lengthInTextElements = info.LengthInTextElements;
     ArrayList list = new ArrayList(this.Links.Count);
     foreach (Link link in this.Links)
     {
         int start = ConvertToCharIndex(link.Start, text);
         int num3 = ConvertToCharIndex(link.Start + link.Length, text);
         if (this.LinkInText(start, num3 - start))
         {
             int num4 = Math.Min(link.Length, lengthInTextElements - link.Start);
             list.Add(new CharacterRange(start, ConvertToCharIndex(link.Start + num4, text) - start));
         }
     }
     CharacterRange[] array = new CharacterRange[list.Count + 1];
     list.CopyTo(array, 0);
     array[array.Length - 1] = new CharacterRange(0, text.Length);
     return array;
 }
Exemple #2
0
 public void String_Set()
 {
     string value = s_randomDataGenerator.GetString(-55, false, MinStringLength, MaxStringLength);
     StringInfo stringInfo = new StringInfo();
     stringInfo.String = value;
     Assert.Equal(value, stringInfo.String);
 }
 public void TestInstancesWithSameArg()
 {
     string str = _generator.GetString(-55, false, c_MINI_STRING_LENGTH, c_MAX_STRING_LENGTH);
     StringInfo stringInfo1 = new StringInfo(str);
     StringInfo stringInfo2 = new StringInfo(str);
     Assert.Equal(stringInfo2.GetHashCode(), stringInfo1.GetHashCode());
 }
Exemple #4
0
 public void TestSameReference()
 {
     string str = _generator.GetString(-55, false, c_MINI_STRING_LENGTH, c_MAX_STRING_LENGTH);
     StringInfo stringInfo1 = new StringInfo(str);
     StringInfo stringInfo2 = stringInfo1;
     Assert.True(stringInfo1.Equals(stringInfo2));
 }
Exemple #5
0
        private List <string> CheckPinyin2(string words, List <string> strList2)
        {
            var w   = new System.Globalization.StringInfo(words);
            var len = w.LengthInTextElements;

            if (len != strList2.Count)
            {
                return(null);
            }

            List <string> list = new List <string>();

            for (int i = 0; i < strList2.Count; i++)
            {
                var py    = strList2[i];
                var rtome = RemoveTone(py);
                var t     = w.SubstringByTextElements(i, 1);
                if (noneTomeDict.TryGetValue(t, out List <string> pys))
                {
                    if (pys.Contains(rtome) == false)
                    {
                        return(null);
                    }
                }
            }

            foreach (var item in strList2)
            {
                var py = CheckPinyin(item);
                list.Add(py);
            }
            return(list);
        }
		public void SubstringByTextElements ()
		{
			StringInfo si = new StringInfo ("A\u0330BC\u0330");
			Assert.AreEqual ("A\u0330BC\u0330", si.SubstringByTextElements (0), "#1");
			Assert.AreEqual ("BC\u0330", si.SubstringByTextElements (1), "#2");
			Assert.AreEqual ("C\u0330", si.SubstringByTextElements (2), "#3");
		}
Exemple #7
0
 public void TestEqualStringInfoWithArg()
 {
     string str = _generator.GetString(-55, false, c_MINI_STRING_LENGTH, c_MAX_STRING_LENGTH);
     StringInfo stringInfo1 = new StringInfo(str);
     StringInfo stringInfo2 = new StringInfo(str);
     Assert.True(stringInfo1.Equals(stringInfo2));
 }
 public void TestDiffInstances()
 {
     string str = TestLibrary.Generator.GetString(-55, false, c_MINI_STRING_LENGTH, c_MAX_STRING_LENGTH);
     StringInfo stringInfo1 = new StringInfo(str);
     StringInfo stringInfo2 = new StringInfo("");
     Assert.NotEqual(stringInfo2.GetHashCode(), stringInfo1.GetHashCode());
 }
Exemple #9
0
 public void TestSetProperty()
 {
     string str = _generator.GetString(-55, false, c_MINI_STRING_LENGTH, c_MAX_STRING_LENGTH);
     StringInfo stringInfo = new StringInfo();
     stringInfo.String = str;
     Assert.Equal(str, stringInfo.String);
 }
Exemple #10
0
 public void TestNullReference()
 {
     string str = null;
     Assert.Throws<ArgumentNullException>(() =>
     {
         StringInfo stringInfo = new StringInfo(str);
     });
 }
Exemple #11
0
 public void Equals(StringInfo stringInfo, object value, bool expected)
 {
     Assert.Equal(expected, stringInfo.Equals(value));
     if (value is StringInfo)
     {
         Assert.Equal(expected, stringInfo.GetHashCode().Equals(value.GetHashCode()));
     }
 }
 public static string CapitalizeFirstLetter(this string s, CultureInfo ci = null)
 {
     var si = new StringInfo(s);
     if (ci == null)
         ci = CultureInfo.CurrentCulture;
     if (si.LengthInTextElements > 0)
         s = si.SubstringByTextElements(0, 1).ToUpper(ci);
     if (si.LengthInTextElements > 1)
         s += si.SubstringByTextElements(1);
     return s;
 }
Exemple #13
0
 public static IEnumerable<object[]> Equals_TestData()
 {
     string randomString = s_randomDataGenerator.GetString(-55, false, MinStringLength, MaxStringLength);
     StringInfo randomStringInfo = new StringInfo(randomString);
     yield return new object[] { randomStringInfo, new StringInfo(randomString), true };
     yield return new object[] { randomStringInfo, randomStringInfo, true };
     yield return new object[] { new StringInfo(), new StringInfo(), true };
     yield return new object[] { new StringInfo("stringinfo1"), new StringInfo("stringinfo2"), false };
     yield return new object[] { new StringInfo("stringinfo1"), "stringinfo1", false };
     yield return new object[] { new StringInfo("stringinfo1"), 123, false };
     yield return new object[] { new StringInfo("stringinfo1"), null, false };
 }
Exemple #14
0
        public void CanCalculateLength()
        {
            var str = "ไม่เอาเห็ด";
            var length = new StringInfo(str).LengthInTextElements;
            Assert.AreEqual(8, length);

            str = "123456";
            length = new StringInfo(str).LengthInTextElements;
            Assert.AreEqual(6, length);

            str = "âl'a";
            length = new StringInfo(str).LengthInTextElements;
            Assert.AreEqual(4, length);
        }
Exemple #15
0
        public static string Truncate(this HtmlHelper helper, string input, int length, string omission)
        {
            // http://dobon.net/vb/dotnet/string/substring.html
            
            StringInfo si = new StringInfo(input);

            if (si.LengthInTextElements <= length)
            {
                return input;
            }
            else
            {
                return si.SubstringByTextElements(0, length) + omission;
            }
        }
    public void SurrogatePairValid()
    {
      string json = @"{ ""MATHEMATICAL ITALIC CAPITAL ALPHA"": ""\uD835\uDEE2"" }";

      JsonTextReader reader = new JsonTextReader(new StringReader(json));

      Assert.IsTrue(reader.Read());
      Assert.IsTrue(reader.Read());

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.String, reader.TokenType);

      string s = reader.Value.ToString();
      Assert.AreEqual(2, s.Length);

      StringInfo stringInfo = new StringInfo(s);
      Assert.AreEqual(1, stringInfo.LengthInTextElements);
    }
Exemple #17
0
        /// <summary>Retrieves a substring of text elements from the current <see cref="T:System.Globalization.StringInfo" /> object starting from a specified text element and continuing through the last text element.</summary>
        /// <returns>A substring of text elements in this <see cref="T:System.Globalization.StringInfo" /> object, starting from the text element index specified by the <paramref name="startingTextElement" /> parameter and continuing through the last text element in this object.</returns>
        /// <param name="startingTextElement">The zero-based index of a text element in this <see cref="T:System.Globalization.StringInfo" /> object.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">
        ///   <paramref name="startingTextElement" /> is less than zero.-or-The string that is the value of the current <see cref="T:System.Globalization.StringInfo" /> object is the empty string ("").</exception>
        public string SubstringByTextElements(int startingTextElement)
        {
            if (startingTextElement < 0 || this.s.Length == 0)
            {
                throw new ArgumentOutOfRangeException("startingTextElement");
            }
            int num = 0;

            for (int i = 0; i < startingTextElement; i++)
            {
                if (num >= this.s.Length)
                {
                    throw new ArgumentOutOfRangeException("startingTextElement");
                }
                num += StringInfo.GetNextTextElementLength(this.s, num);
            }
            return(this.s.Substring(num));
        }
Exemple #18
0
        public bool MoveNext()
        {
            elementindex = index + 1;

            if (elementindex < str.Length)
            {
                element = StringInfo.GetNextTextElement(str, elementindex);
                index  += element.Length;

                return(true);
            }
            else
            {
                element = null;

                return(false);
            }
        }
Exemple #19
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="text">Target of string. Should be normalized 'NormalizationForm.FormC'</param>
        /// <returns></returns>
        public int GetTweetLength(String text)
        {
            try
            {
                //text = text.Normalize(NormalizationForm.FormC);
            }
            catch { }

            int length = new StringInfo(text).LengthInTextElements;
            foreach (Extractor.Entity urlEntity in __Extractor.ExtractURLsWithIndices(text))
            {
                // Subtract the length of the original URL
                length -= (urlEntity.End - urlEntity.Start);

                // Add `ShortUrlLengthHttps` characters for URL starting with https:// Otherwise add `ShortUrlLength` characters
                length += urlEntity.Value.ToLower().StartsWith("https://") ? ShortUrlLengthHttps : ShortUrlLength;
            }
            return length;
        }
Exemple #20
0
        public static string GetNextTextElement(string str, int index)
        {
            if (str == null)
            {
                throw new ArgumentNullException("str");
            }
            int length = str.Length;

            if (index >= 0 && index < length)
            {
                int             num;
                UnicodeCategory unicodeCategory = CharUnicodeInfo.InternalGetUnicodeCategory(str, index, out num);
                return(str.Substring(index, StringInfo.GetCurrentTextElementLen(str, index, length, ref unicodeCategory, ref num)));
            }
            if (index == length)
            {
                return(string.Empty);
            }
            throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
        }
    /// <summary>Returns a string array that contains the substrings in this string that are seperated a given fixed length.</summary>
    /// <param name="s">This string object.</param>
    /// <param name="length">Size of each substring.
    /// <para>CASE: length &gt; 0 , RESULT: String is split from left to right.</para>
    /// <para>CASE: length == 0 , RESULT: String is returned as the only entry in the array.</para>
    /// <para>CASE: length &lt; 0 , RESULT: String is split from right to left.</para>
    /// </param>
    /// <returns>String array that has been split into substrings of equal length.</returns>
    /// <example>
    /// <code>
    /// string s = "1234567890";
    /// string[] a = s.Split(4); // a == { "1234", "5678", "90" }
    /// </code>
    /// </example>
    public static string[] Split(this string s, int length)
    {
        System.Globalization.StringInfo str = new System.Globalization.StringInfo(s);

        int lengthAbs = Math.Abs(length);

        if (str.LengthInTextElements == 0 || lengthAbs == 0 || str.LengthInTextElements <= lengthAbs)
        {
            return(new string[] { str.String });
        }

        string[] array =
            new string[
                (str.LengthInTextElements % lengthAbs == 0
                     ? str.LengthInTextElements / lengthAbs
                     : (str.LengthInTextElements / lengthAbs) + 1)];

        if (length > 0)
        {
            for (int iStr = 0, iArray = 0; iStr < str.LengthInTextElements && iArray < array.Length; iStr += lengthAbs, iArray++)
            {
                array[iArray] = str.SubstringByTextElements(
                    iStr, (str.LengthInTextElements - iStr < lengthAbs ? str.LengthInTextElements - iStr : lengthAbs));
            }
        }

        else
        {
            for (int iStr = str.LengthInTextElements - 1, iArray = array.Length - 1;
                 iStr >= 0 && iArray >= 0;
                 iStr -= lengthAbs, iArray--)
            {
                array[iArray] = str.SubstringByTextElements(
                    (iStr - lengthAbs < 0 ? 0 : iStr - lengthAbs + 1), (iStr - lengthAbs < 0 ? iStr + 1 : lengthAbs));
            }
        }

        return(array);
    }
Exemple #22
0
        /// <devdoc>
        ///     Converts the character index into char index of the string
        ///     This method is copied in LinkCollectionEditor.cs. Update the other
        ///     one as well if you change this method.
        ///     This method mainly deal with surrogate. Suppose we 
        ///     have a string consisting of 3 surrogates, and we want the
        ///     second character, then the index we need should be 2 instead of
        ///     1, and this method returns the correct index.
        /// </devdoc>
        private static int ConvertToCharIndex(int index, string text) {
            if (index <= 0) {
                return 0;
            }
            if (String.IsNullOrEmpty(text)) {
                Debug.Assert(text != null, "string should not be null"); 
                //do no conversion, just return the original value passed in
                return index;
            }

            //VSWhidbey 217272: Dealing with surrogate characters
            //in some languages, characters can expand over multiple
            //chars, using StringInfo lets us properly deal with it.
            StringInfo stringInfo = new StringInfo(text);
            int numTextElements = stringInfo.LengthInTextElements;

            //index is greater than the length of the string
            if (index > numTextElements) {
                return index - numTextElements + text.Length;  //pretend all the characters after are ASCII characters
            }
            //return the length of the substring which has specified number of characters
            string sub = stringInfo.SubstringByTextElements(0, index);
            return sub.Length;
        }
Exemple #23
0
        /// <devdoc>
        ///     Calculate character ranges taking into account the locale.  Provided for surrogate chars support.
        /// </devdoc>
        private CharacterRange[] AdjustCharacterRangesForSurrogateChars(){
            string text = Text;

            if (String.IsNullOrEmpty(text)) {
                return new CharacterRange[]{};
            }

            StringInfo stringInfo = new StringInfo(text);                 
            int textLen = stringInfo.LengthInTextElements;
            ArrayList ranges = new ArrayList(Links.Count);

            foreach (Link link in Links) {
                int charStart = ConvertToCharIndex(link.Start, text);
                int charEnd = ConvertToCharIndex(link.Start + link.Length, text);
                if (LinkInText(charStart, charEnd - charStart)) {
                    int length = (int) Math.Min(link.Length, textLen - link.Start);
                    ranges.Add(new CharacterRange(charStart, ConvertToCharIndex(link.Start + length, text) - charStart));
                }
            }

            CharacterRange[] regions = new CharacterRange[ranges.Count + 1];
            ranges.CopyTo(regions, 0);
            regions[regions.Length - 1] = new CharacterRange(0, text.Length);

            return regions;
        }
Exemple #24
0
 public void TestCtorWithWhitespace()
 {
     string str = " ";
     StringInfo stringInfo = new StringInfo(str);
     Assert.Equal(" ", stringInfo.String);
 }
Exemple #25
0
        /// <summary>指定した文字列の文字列情報を取得する。</summary>
        /// <param name="input">入力文字列</param>
        /// <param name="s_length">指定した文字列の文字数(サロゲート ペア文字未対応)</param>
        /// <param name="si_length">指定した文字列の文字数(サロゲート ペア文字対応)</param>
        /// <param name="byte_length">UTF-16(Unicode)でのバイト長</param>
        public void GetStringInfo(string input, out int s_length, out int si_length, out int byte_length)
        {
            // System.Globalization.StringInfoを使用する。
            StringInfo si = new StringInfo(input);

            // 文字数(Length)
            s_length = input.Length;

            // 文字数(LengthInTextElements)
            si_length = si.LengthInTextElements;

            // UTF-16(Unicode)でのバイト長
            byte_length = Encoding.Unicode.GetBytes(input).Length;
        }
Exemple #26
0
 public static string GetNextTextElement(string str)
 {
     return(StringInfo.GetNextTextElement(str, 0));
 }
 private bool IsOneLink()
 {
     if (((this.links == null) || (this.links.Count != 1)) || (this.Text == null))
     {
         return false;
     }
     StringInfo info = new StringInfo(this.Text);
     return ((this.LinkArea.Start == 0) && (this.LinkArea.Length == info.LengthInTextElements));
 }
Exemple #28
0
 public void Ctor_Empty()
 {
     StringInfo stringInfo = new StringInfo();
     Assert.Equal(string.Empty, stringInfo.String);
 }
        public override bool Equals(object value)
        {
            StringInfo info = value as StringInfo;

            return((info != null) && this.m_str.Equals(info.m_str));
        }
 private void VerificationHelper(string str, int expected)
 {
     StringInfo stringInfo = new StringInfo(str);
     int result = stringInfo.LengthInTextElements;
     Assert.Equal(expected, result);
 }
        public override bool Equals(object value)
        {
            StringInfo other = value as StringInfo;

            return(other != null && s == other.s);
        }
        /// <summary>
        /// Gets display name of a property.
        /// For example:
        /// ID => ID
        /// EmployeeName => Employee Name
        /// EmployeeXMLName => Employee XML Name
        /// EmployeeXML => Employee XML
        /// Employee_XML => Employee XML
        /// </summary>
        /// <param name="propertyName">The property name.</param>
        /// <returns>Display name</returns>
        public static string GetPropertyDisplayName(string propertyName)
        {
            Debug.Assert(!string.IsNullOrEmpty(propertyName));

            if (string.IsNullOrEmpty(propertyName)) return "";

            StringInfo propertyNameInfo = new StringInfo(propertyName);
            StringBuilder nameBuilder = new StringBuilder();
            string currentUpperCaseSection = "";
            for (int i = 0; i < propertyNameInfo.LengthInTextElements; i++)
            {
                bool hasContentBefore = nameBuilder.Length > 0;
                string current = propertyNameInfo.SubstringByTextElements(i, 1);
                bool currentIsUpper = Char.IsUpper(current, 0);
                if (currentIsUpper)
                {
                    if (hasContentBefore && currentUpperCaseSection.Length == 0)
                    {
                        currentUpperCaseSection = " ";
                    }
                    currentUpperCaseSection = currentUpperCaseSection + current;
                    continue;
                }
                else if (currentUpperCaseSection.Length > 0)
                {
                    currentUpperCaseSection = currentUpperCaseSection.TrimEnd();
                    if (currentUpperCaseSection.Length > 2)
                    {
                        nameBuilder.Append(currentUpperCaseSection.Substring(0, currentUpperCaseSection.Length - 1));
                        nameBuilder.Append(" ");
                        currentUpperCaseSection = currentUpperCaseSection.Substring(currentUpperCaseSection.Length - 1);
                    }
                    nameBuilder.Append(currentUpperCaseSection);
                    currentUpperCaseSection = "";
                }

                if (string.Equals(current, "_", StringComparison.OrdinalIgnoreCase))
                {
                    // Convert "_" as " "
                    if (hasContentBefore && currentUpperCaseSection.Length == 0)
                    {
                        currentUpperCaseSection = " ";
                    }
                    continue;
                }
                
                nameBuilder.Append(current);
            }

            if (currentUpperCaseSection.Length > 0)
            {
                nameBuilder.Append(currentUpperCaseSection.TrimEnd());
            }

            return nameBuilder.ToString();
        }
Exemple #33
0
        public override bool Equals(object obj)
        {
            StringInfo other = obj as StringInfo;

            return(other != null && s == other.s);
        }
 private static int ConvertToCharIndex(int index, string text)
 {
     if (index <= 0)
     {
         return 0;
     }
     if (string.IsNullOrEmpty(text))
     {
         return index;
     }
     StringInfo info = new StringInfo(text);
     int lengthInTextElements = info.LengthInTextElements;
     if (index > lengthInTextElements)
     {
         return ((index - lengthInTextElements) + text.Length);
     }
     return info.SubstringByTextElements(0, index).Length;
 }
Exemple #35
0
 /// <devdoc>
 ///     Determines whether the whole link label contains only one link,
 ///     and the link runs from the beginning of the label to the end of it
 /// </devdoc>
 private bool IsOneLink() {
     if (links == null || links.Count != 1 || Text == null) {
         return false;
     }
     StringInfo stringInfo = new StringInfo(Text);
     if (LinkArea.Start == 0 && LinkArea.Length == stringInfo.LengthInTextElements) {
         return true;
     }
     return false;
 }
Exemple #36
0
 public void Ctor_String(string value)
 {
     var stringInfo = new StringInfo(value);
     Assert.Equal(value, stringInfo.String);
 }
        private bool ValidateString(JSchema schema, string value)
        {
            if (!TestType(schema, JSchemaType.String, value))
                return false;

            if (schema.MaximumLength != null || schema.MinimumLength != null)
            {
                // want to test the character length and ignore unicode surrogates
                StringInfo stringInfo = new StringInfo(value);
                int textLength = stringInfo.LengthInTextElements;

                if (schema.MaximumLength != null && textLength > schema.MaximumLength)
                    RaiseError($"String '{value}' exceeds maximum length of {schema.MaximumLength}.", ErrorType.MaximumLength, schema, value, null);

                if (schema.MinimumLength != null && textLength < schema.MinimumLength)
                    RaiseError($"String '{value}' is less than minimum length of {schema.MinimumLength}.", ErrorType.MinimumLength, schema, value, null);
            }

            if (schema.Pattern != null)
            {
                Regex regex;
                string errorMessage;

                if (schema.TryGetPatternRegex(out regex, out errorMessage))
                {
                    if (!regex.IsMatch(value))
                        RaiseError($"String '{value}' does not match regex pattern '{schema.Pattern}'.", ErrorType.Pattern, schema, value, null);
                }
                else
                {
                    RaiseError($"Could not validate string with regex pattern '{schema.Pattern}'. There was an error parsing the regex: {errorMessage}", ErrorType.Pattern, schema, value, null);
                }
            }

            if (schema.Format != null)
            {
                bool valid = ValidateFormat(schema.Format, value);

                if (!valid)
                    RaiseError($"String '{value}' does not validate against format '{schema.Format}'.", ErrorType.Format, schema, value, null);
            }

            return true;
        }
        /// <summary>
        /// ステータス更新を行う
        /// </summary>
        /// <param name="text"></param>
        private void UpdateStatus(string text)
        {
            // 140字以下かどうかのチェックを行い、超えている場合はconfigに従って動作する
            StringInfo stringInfo = new StringInfo(text);
            int tweetLength = stringInfo.LengthInTextElements;

            if (tweetLength < 140)
            {
                oauth.UpdateStatus(text, this.inReplyToStatusId);
            }
            else
            {
                string subText = "";
                switch (config.TreatTooLongTweetAs)
                {
                    case 1:
                        subText = stringInfo.SubstringByTextElements(0, 137);
                        subText += "...";

                        TweetTextBox.Enabled = false;
                        oauth.UpdateStatus(subText, this.inReplyToStatusId);
                        break;
                    case 2:
                        subText = stringInfo.SubstringByTextElements(0, 133);
                        subText += "[...続く]";
                        oauth.UpdateStatus(subText, this.inReplyToStatusId);

                        UpdateStatus(stringInfo.SubstringByTextElements(133));
                        break;
                    default:
                        KumaHodaiToolStripStatusLabel.Text = "エラー: 文字数が140文字を超えています。";
                        break;
                }
            }
        }
Exemple #39
0
 public static TextElementEnumerator GetTextElementEnumerator(string str)
 {
     return(StringInfo.GetTextElementEnumerator(str, 0));
 }
 public static string ValidateWithStringInfo(string input)
 {
     StringInfo si = new StringInfo(input);
     return si.String;
 }
Exemple #41
0
        public override bool Equals(object value)
        {
            StringInfo stringInfo = value as StringInfo;

            return(stringInfo != null && this.s == stringInfo.s);
        }
Exemple #42
0
        /// <summary>Gets the text element at the specified index of the specified string.</summary>
        /// <returns>A string containing the text element at the specified index of the specified string.</returns>
        /// <param name="str">The string from which to get the text element. </param>
        /// <param name="index">The zero-based index at which the text element starts. </param>
        /// <exception cref="T:System.ArgumentNullException">
        ///   <paramref name="str" /> is null. </exception>
        /// <exception cref="T:System.ArgumentOutOfRangeException">
        ///   <paramref name="index" /> is outside the range of valid indexes for <paramref name="str" />. </exception>
        public static string GetNextTextElement(string str, int index)
        {
            int nextTextElementLength = StringInfo.GetNextTextElementLength(str, index);

            return((nextTextElementLength == 1) ? new string(str[index], 1) : str.Substring(index, nextTextElementLength));
        }