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