1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is TransforMiiX XSLT processor code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 2002
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Peter Van der Beken <peterv@propagandism.org>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #ifndef __nsWhitespaceTokenizer_h
40 : #define __nsWhitespaceTokenizer_h
41 :
42 : #include "nsDependentSubstring.h"
43 :
44 : class nsWhitespaceTokenizer
45 : {
46 : public:
47 0 : nsWhitespaceTokenizer(const nsSubstring& aSource)
48 : {
49 0 : aSource.BeginReading(mIter);
50 0 : aSource.EndReading(mEnd);
51 :
52 0 : while (mIter != mEnd && isWhitespace(*mIter)) {
53 0 : ++mIter;
54 : }
55 0 : }
56 :
57 : /**
58 : * Checks if any more tokens are available.
59 : */
60 0 : bool hasMoreTokens()
61 : {
62 0 : return mIter != mEnd;
63 : }
64 :
65 : /**
66 : * Returns the next token.
67 : */
68 0 : const nsDependentSubstring nextToken()
69 : {
70 0 : nsSubstring::const_char_iterator begin = mIter;
71 0 : while (mIter != mEnd && !isWhitespace(*mIter)) {
72 0 : ++mIter;
73 : }
74 0 : nsSubstring::const_char_iterator end = mIter;
75 0 : while (mIter != mEnd && isWhitespace(*mIter)) {
76 0 : ++mIter;
77 : }
78 0 : return Substring(begin, end);
79 : }
80 :
81 : private:
82 : nsSubstring::const_char_iterator mIter, mEnd;
83 :
84 0 : bool isWhitespace(PRUnichar aChar)
85 : {
86 : return aChar <= ' ' &&
87 : (aChar == ' ' || aChar == '\n' ||
88 0 : aChar == '\r'|| aChar == '\t');
89 : }
90 : };
91 :
92 : class nsCWhitespaceTokenizer
93 : {
94 : public:
95 1961 : nsCWhitespaceTokenizer(const nsCSubstring& aSource)
96 : {
97 1961 : aSource.BeginReading(mIter);
98 1961 : aSource.EndReading(mEnd);
99 :
100 3922 : while (mIter != mEnd && isWhitespace(*mIter)) {
101 0 : ++mIter;
102 : }
103 1961 : }
104 :
105 : /**
106 : * Checks if any more tokens are available.
107 : */
108 3159 : bool hasMoreTokens()
109 : {
110 3159 : return mIter != mEnd;
111 : }
112 :
113 : /**
114 : * Returns the next token.
115 : */
116 2132 : const nsDependentCSubstring nextToken()
117 : {
118 2132 : nsCSubstring::const_char_iterator begin = mIter;
119 12096 : while (mIter != mEnd && !isWhitespace(*mIter)) {
120 7832 : ++mIter;
121 : }
122 2132 : nsCSubstring::const_char_iterator end = mIter;
123 4841 : while (mIter != mEnd && isWhitespace(*mIter)) {
124 577 : ++mIter;
125 : }
126 2132 : return Substring(begin, end);
127 : }
128 :
129 : private:
130 : nsCSubstring::const_char_iterator mIter, mEnd;
131 :
132 11361 : bool isWhitespace(char aChar)
133 : {
134 : return aChar <= ' ' &&
135 : (aChar == ' ' || aChar == '\n' ||
136 11361 : aChar == '\r'|| aChar == '\t');
137 : }
138 : };
139 :
140 : #endif /* __nsWhitespaceTokenizer_h */
|