#region Apache Notice /***************************************************************************** * $Header: $ * $Revision: $ * $Date: $ * * iBATIS.NET Data Mapper * Copyright (C) 2004 - Gilles Bayon * * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ********************************************************************************/ #endregion using System; using System.Collections; using System.Text; namespace IBatisNet.Common.Utilities { /// /// A StringTokenizer java like object /// public class StringTokenizer : IEnumerable { private static readonly string _defaultDelim=" \t\n\r\f"; string _origin = string.Empty; string _delimiters = string.Empty; bool _returnDelimiters = false; /// /// Constructs a StringTokenizer on the specified String, using the /// default delimiter set (which is " \t\n\r\f"). /// /// The input String public StringTokenizer(string str) { _origin = str; _delimiters = _defaultDelim; _returnDelimiters = false; } /// /// Constructs a StringTokenizer on the specified String, /// using the specified delimiter set. /// /// The input String /// The delimiter String public StringTokenizer(string str, string delimiters) { _origin = str; _delimiters = delimiters; _returnDelimiters = false; } /// /// Constructs a StringTokenizer on the specified String, /// using the specified delimiter set. /// /// The input String /// The delimiter String /// Returns delimiters as tokens or skip them public StringTokenizer(string str, string delimiters, bool returnDelimiters) { _origin = str; _delimiters = delimiters; _returnDelimiters = returnDelimiters; } /// /// /// /// public IEnumerator GetEnumerator() { return new StringTokenizerEnumerator(this); } /// /// Returns the number of tokens in the String using /// the current deliminter set. This is the number of times /// nextToken() can return before it will generate an exception. /// Use of this routine to count the number of tokens is faster /// than repeatedly calling nextToken() because the substrings /// are not constructed and returned for each token. /// public int TokenNumber { get { int count = 0; int currpos = 0; int maxPosition = _origin.Length; while (currpos < maxPosition) { while (!_returnDelimiters && (currpos < maxPosition) && (_delimiters.IndexOf(_origin[currpos]) >= 0)) { currpos++; } if (currpos >= maxPosition) { break; } int start = currpos; while ((currpos < maxPosition) && (_delimiters.IndexOf(_origin[currpos]) < 0)) { currpos++; } if (_returnDelimiters && (start == currpos) && (_delimiters.IndexOf(_origin[currpos]) >= 0)) { currpos++; } count++; } return count; } } private class StringTokenizerEnumerator : IEnumerator { private StringTokenizer _stokenizer; private int _cursor = 0; private string _next = null; public StringTokenizerEnumerator(StringTokenizer stok) { _stokenizer = stok; } public bool MoveNext() { _next = GetNext(); return _next != null; } public void Reset() { _cursor = 0; } public object Current { get { return _next; } } private string GetNext() { char c; bool isDelim; if( _cursor >= _stokenizer._origin.Length ) return null; c = _stokenizer._origin[_cursor]; isDelim = (_stokenizer._delimiters.IndexOf(c) != -1); if ( isDelim ) { _cursor++; if ( _stokenizer._returnDelimiters ) { return c.ToString(); } return GetNext(); } int nextDelimPos = _stokenizer._origin.IndexOfAny(_stokenizer._delimiters.ToCharArray(), _cursor); if (nextDelimPos == -1) { nextDelimPos = _stokenizer._origin.Length; } string nextToken = _stokenizer._origin.Substring(_cursor, nextDelimPos - _cursor); _cursor = nextDelimPos; return nextToken; } } } }