TextElementEnumerator.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // System.Globalization.TextElementEnumerator.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 TextElementEnumerator: IEnumerator {
  36. private int index;
  37. private int elementindex;
  38. private int startpos;
  39. private string str;
  40. private string element;
  41. /* Hide the .ctor() */
  42. internal TextElementEnumerator(string str, int startpos) {
  43. this.index = -1;
  44. this.startpos = startpos;
  45. this.str = str.Substring (startpos);
  46. this.element = null;
  47. }
  48. public object Current
  49. {
  50. get {
  51. if (element == null) {
  52. throw new InvalidOperationException ();
  53. }
  54. return(element);
  55. }
  56. }
  57. public int ElementIndex
  58. {
  59. get {
  60. if (element == null) {
  61. throw new InvalidOperationException ();
  62. }
  63. return(elementindex + startpos);
  64. }
  65. }
  66. public string GetTextElement()
  67. {
  68. if (element == null) {
  69. throw new InvalidOperationException ();
  70. }
  71. return(element);
  72. }
  73. public bool MoveNext()
  74. {
  75. elementindex = index + 1;
  76. if (elementindex < str.Length) {
  77. element = StringInfo.GetNextTextElement (str, elementindex);
  78. index += element.Length;
  79. return(true);
  80. } else {
  81. element = null;
  82. return(false);
  83. }
  84. }
  85. public void Reset()
  86. {
  87. element = null;
  88. index = -1;
  89. }
  90. }
  91. }