StringInfo.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // System.Globalization.StringInfo.cs
  3. //
  4. // Author:
  5. // Dick Porter ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc.
  8. // (C) 2004 Novell, Inc.
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System.Collections;
  33. namespace System.Globalization {
  34. [Serializable]
  35. public class StringInfo {
  36. public StringInfo()
  37. {
  38. }
  39. public static string GetNextTextElement(string str)
  40. {
  41. if(str == null || str.Length == 0) {
  42. throw new ArgumentNullException("string is null");
  43. }
  44. return(GetNextTextElement (str, 0));
  45. }
  46. public static string GetNextTextElement(string str, int index)
  47. {
  48. if(str == null) {
  49. throw new ArgumentNullException("string is null");
  50. }
  51. if(index < 0 || index >= str.Length) {
  52. throw new ArgumentOutOfRangeException ("Index is not valid");
  53. }
  54. /* Find the next base character, surrogate
  55. * pair or combining character sequence
  56. */
  57. char ch = str[index];
  58. UnicodeCategory cat = char.GetUnicodeCategory (ch);
  59. if (cat == UnicodeCategory.Surrogate) {
  60. /* Check that it's a high surrogate
  61. * followed by a low surrogate
  62. */
  63. if (ch >= 0xD800 && ch <= 0xDBFF) {
  64. if ((index + 1) < str.Length &&
  65. str[index + 1] >= 0xDC00 &&
  66. str[index + 1] <= 0xDFFF) {
  67. /* A valid surrogate pair */
  68. return(str.Substring (index, 2));
  69. } else {
  70. /* High surrogate on its own */
  71. return(new String (ch, 1));
  72. }
  73. } else {
  74. /* Low surrogate on its own */
  75. return(new String (ch, 1));
  76. }
  77. } else {
  78. /* Look for a base character, which
  79. * may or may not be followed by a
  80. * series of combining characters
  81. */
  82. if (cat == UnicodeCategory.NonSpacingMark ||
  83. cat == UnicodeCategory.SpacingCombiningMark ||
  84. cat == UnicodeCategory.EnclosingMark) {
  85. /* Not a base character */
  86. return(new String (ch, 1));
  87. }
  88. int count = 1;
  89. while (index + count < str.Length) {
  90. cat = char.GetUnicodeCategory (str[index + count]);
  91. if (cat != UnicodeCategory.NonSpacingMark &&
  92. cat != UnicodeCategory.SpacingCombiningMark &&
  93. cat != UnicodeCategory.EnclosingMark) {
  94. /* Finished the sequence */
  95. break;
  96. }
  97. count++;
  98. }
  99. return(str.Substring (index, count));
  100. }
  101. }
  102. public static TextElementEnumerator GetTextElementEnumerator(string str)
  103. {
  104. if(str == null || str.Length == 0) {
  105. throw new ArgumentNullException("string is null");
  106. }
  107. return(new TextElementEnumerator (str, 0));
  108. }
  109. public static TextElementEnumerator GetTextElementEnumerator(string str, int index)
  110. {
  111. if(str == null) {
  112. throw new ArgumentNullException("string is null");
  113. }
  114. if(index < 0 || index >= str.Length) {
  115. throw new ArgumentOutOfRangeException ("Index is not valid");
  116. }
  117. return(new TextElementEnumerator (str, index));
  118. }
  119. public static int[] ParseCombiningCharacters(string str)
  120. {
  121. if(str == null) {
  122. throw new ArgumentNullException("string is null");
  123. }
  124. ArrayList indices = new ArrayList (str.Length);
  125. TextElementEnumerator tee = GetTextElementEnumerator (str);
  126. tee.Reset ();
  127. while(tee.MoveNext ()) {
  128. indices.Add (tee.ElementIndex);
  129. }
  130. return((int[])indices.ToArray (typeof (int)));
  131. }
  132. }
  133. }