root/apps/outlook/branches/1.5/DotTelSystem/Punycode/generate/Tokenizer.cs
@
580
| Revision 580, 7.6 kB (checked in by jonmaycock, 10 months ago) |
|---|
| Line | |
|---|---|
| 1 | /// <summary> Copyright (C) 2004 Free Software Foundation, Inc. |
| 2 | /// * |
| 3 | /// Author: Alexander Gnauck AG-Software |
| 4 | /// * |
| 5 | /// This file is part of GNU Libidn. |
| 6 | /// * |
| 7 | /// This library is free software; you can redistribute it and/or |
| 8 | /// modify it under the terms of the GNU Lesser General Public License |
| 9 | /// as published by the Free Software Foundation; either version 2.1 of |
| 10 | /// the License, or (at your option) any later version. |
| 11 | /// * |
| 12 | /// This library is distributed in the hope that it will be useful, but |
| 13 | /// WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | /// Lesser General Public License for more details. |
| 16 | /// * |
| 17 | /// You should have received a copy of the GNU Lesser General Public |
| 18 | /// License along with this library; if not, write to the Free Software |
| 19 | /// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 20 | /// USA |
| 21 | /// </summary> |
| 22 | using System; |
| 23 | using System.Collections; |
| 24 | using System.Text; |
| 25 | |
| 26 | namespace gnu.inet.encoding.misc |
| 27 | { |
| 28 | /// <summary> |
| 29 | /// The class performs token processing in strings |
| 30 | /// </summary> |
| 31 | public class Tokenizer : IEnumerator |
| 32 | { |
| 33 | /// Position over the string |
| 34 | private long currentPos = 0; |
| 35 | |
| 36 | /// Include demiliters in the results. |
| 37 | private bool includeDelims = false; |
| 38 | |
| 39 | /// Char representation of the String to tokenize. |
| 40 | private char[] chars = null; |
| 41 | |
| 42 | //The tokenizer uses the default delimiter set: the space character, the tab character, the newline character, and the carriage-return character and the form-feed character |
| 43 | private string delimiters = " \t\n\r\f"; |
| 44 | |
| 45 | /// <summary> |
| 46 | /// Initializes a new class instance with a specified string to process |
| 47 | /// </summary> |
| 48 | /// <param name="source">String to tokenize</param> |
| 49 | public Tokenizer(string source) |
| 50 | { |
| 51 | this.chars = source.ToCharArray(); |
| 52 | } |
| 53 | |
| 54 | /// <summary> |
| 55 | /// Initializes a new class instance with a specified string to process |
| 56 | /// and the specified token delimiters to use |
| 57 | /// </summary> |
| 58 | /// <param name="source">String to tokenize</param> |
| 59 | /// <param name="delimiters">String containing the delimiters</param> |
| 60 | public Tokenizer(string source, string delimiters) |
| 61 | : this(source) |
| 62 | { |
| 63 | this.delimiters = delimiters; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | /// <summary> |
| 68 | /// Initializes a new class instance with a specified string to process, the specified token |
| 69 | /// delimiters to use, and whether the delimiters must be included in the results. |
| 70 | /// </summary> |
| 71 | /// <param name="source">String to tokenize</param> |
| 72 | /// <param name="delimiters">String containing the delimiters</param> |
| 73 | /// <param name="includeDelims">Determines if delimiters are included in the results.</param> |
| 74 | public Tokenizer(string source, string delimiters, bool includeDelims) |
| 75 | : this(source, delimiters) |
| 76 | { |
| 77 | this.includeDelims = includeDelims; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | /// <summary> |
| 82 | /// Returns the next token from the token list |
| 83 | /// </summary> |
| 84 | /// <returns>The string value of the token</returns> |
| 85 | public string NextToken() |
| 86 | { |
| 87 | return NextToken(this.delimiters); |
| 88 | } |
| 89 | |
| 90 | /// <summary> |
| 91 | /// Returns the next token from the source string, using the provided |
| 92 | /// token delimiters |
| 93 | /// </summary> |
| 94 | /// <param name="delimiters">String containing the delimiters to use</param> |
| 95 | /// <returns>The string value of the token</returns> |
| 96 | public string NextToken(string delimiters) |
| 97 | { |
| 98 | //According to documentation, the usage of the received delimiters should be temporary (only for this call). |
| 99 | //However, it seems it is not true, so the following line is necessary. |
| 100 | this.delimiters = delimiters; |
| 101 | |
| 102 | //at the end |
| 103 | if (this.currentPos == this.chars.Length) |
| 104 | throw new System.ArgumentOutOfRangeException(); |
| 105 | //if over a delimiter and delimiters must be returned |
| 106 | else if ((System.Array.IndexOf(delimiters.ToCharArray(), chars[this.currentPos]) != -1) |
| 107 | && this.includeDelims) |
| 108 | return "" + this.chars[this.currentPos++]; |
| 109 | //need to get the token wo delimiters. |
| 110 | else |
| 111 | return nextToken(delimiters.ToCharArray()); |
| 112 | } |
| 113 | |
| 114 | //Returns the nextToken wo delimiters |
| 115 | private string nextToken(char[] delimiters) |
| 116 | { |
| 117 | string token = ""; |
| 118 | long pos = this.currentPos; |
| 119 | |
| 120 | //skip possible delimiters |
| 121 | while (System.Array.IndexOf(delimiters, this.chars[currentPos]) != -1) |
| 122 | //The last one is a delimiter (i.e there is no more tokens) |
| 123 | if (++this.currentPos == this.chars.Length) |
| 124 | { |
| 125 | this.currentPos = pos; |
| 126 | throw new System.ArgumentOutOfRangeException(); |
| 127 | } |
| 128 | |
| 129 | //getting the token |
| 130 | while (System.Array.IndexOf(delimiters, this.chars[this.currentPos]) == -1) |
| 131 | { |
| 132 | // don't use += to work around bug in compiler |
| 133 | // see https://bugzilla.novell.com/show_bug.cgi?id=372483 |
| 134 | token = token + this.chars[this.currentPos]; |
| 135 | //the last one is not a delimiter |
| 136 | if (++this.currentPos == this.chars.Length) |
| 137 | break; |
| 138 | } |
| 139 | return token; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | /// <summary> |
| 144 | /// Determines if there are more tokens to return from the source string |
| 145 | /// </summary> |
| 146 | /// <returns>True or false, depending if there are more tokens</returns> |
| 147 | public bool HasMoreTokens() |
| 148 | { |
| 149 | //keeping the current pos |
| 150 | long pos = this.currentPos; |
| 151 | |
| 152 | try |
| 153 | { |
| 154 | this.NextToken(); |
| 155 | } |
| 156 | catch (System.ArgumentOutOfRangeException) |
| 157 | { |
| 158 | return false; |
| 159 | } |
| 160 | finally |
| 161 | { |
| 162 | this.currentPos = pos; |
| 163 | } |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | /// <summary> |
| 168 | /// Remaining tokens count |
| 169 | /// </summary> |
| 170 | public int Count |
| 171 | { |
| 172 | get |
| 173 | { |
| 174 | //keeping the current pos |
| 175 | long pos = this.currentPos; |
| 176 | int i = 0; |
| 177 | |
| 178 | try |
| 179 | { |
| 180 | while (true) |
| 181 | { |
| 182 | this.NextToken(); |
| 183 | i++; |
| 184 | } |
| 185 | } |
| 186 | catch (System.ArgumentOutOfRangeException) |
| 187 | { |
| 188 | this.currentPos = pos; |
| 189 | return i; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /// <summary> |
| 195 | /// Performs the same action as NextToken. |
| 196 | /// </summary> |
| 197 | public System.Object Current |
| 198 | { |
| 199 | get |
| 200 | { |
| 201 | return (Object)this.NextToken(); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /// <summary> |
| 206 | // Performs the same action as HasMoreTokens. |
| 207 | /// </summary> |
| 208 | /// <returns>True or false, depending if there are more tokens</returns> |
| 209 | public bool MoveNext() |
| 210 | { |
| 211 | return this.HasMoreTokens(); |
| 212 | } |
| 213 | |
| 214 | /// <summary> |
| 215 | /// Does nothing. |
| 216 | /// </summary> |
| 217 | public void Reset() |
| 218 | { |
| 219 | ; |
| 220 | } |
| 221 | } |
| 222 | } |
Note: See TracBrowser
for help on using the browser.








