|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef mozilla_CharTokenizer_h |
|
7 #define mozilla_CharTokenizer_h |
|
8 |
|
9 #include "nsDependentSubstring.h" |
|
10 |
|
11 namespace mozilla { |
|
12 |
|
13 template<char16_t delimiter> |
|
14 class CharTokenizer |
|
15 { |
|
16 public: |
|
17 CharTokenizer(const nsSubstring& aSource) |
|
18 { |
|
19 aSource.BeginReading(mIter); |
|
20 aSource.EndReading(mEnd); |
|
21 } |
|
22 |
|
23 /** |
|
24 * Checks if any more tokens are available. |
|
25 */ |
|
26 bool hasMoreTokens() |
|
27 { |
|
28 return mIter != mEnd; |
|
29 } |
|
30 |
|
31 /** |
|
32 * Returns the next token. |
|
33 */ |
|
34 const nsDependentSubstring nextToken() |
|
35 { |
|
36 nsSubstring::const_char_iterator begin = mIter; |
|
37 while (mIter != mEnd && (*mIter) != delimiter) { |
|
38 ++mIter; |
|
39 } |
|
40 nsSubstring::const_char_iterator end = mIter; |
|
41 if (mIter != mEnd) { |
|
42 ++mIter; |
|
43 } |
|
44 |
|
45 return Substring(begin, end); |
|
46 } |
|
47 |
|
48 private: |
|
49 nsSubstring::const_char_iterator mIter, mEnd; |
|
50 }; |
|
51 |
|
52 template<char delimiter> |
|
53 class CCharTokenizer |
|
54 { |
|
55 public: |
|
56 CCharTokenizer(const nsCSubstring& aSource) |
|
57 { |
|
58 aSource.BeginReading(mIter); |
|
59 aSource.EndReading(mEnd); |
|
60 } |
|
61 |
|
62 /** |
|
63 * Checks if any more tokens are available. |
|
64 */ |
|
65 bool hasMoreTokens() |
|
66 { |
|
67 return mIter != mEnd; |
|
68 } |
|
69 |
|
70 /** |
|
71 * Returns the next token. |
|
72 */ |
|
73 const nsDependentCSubstring nextToken() |
|
74 { |
|
75 nsCSubstring::const_char_iterator begin = mIter; |
|
76 while (mIter != mEnd && (*mIter) != delimiter) { |
|
77 ++mIter; |
|
78 } |
|
79 nsCSubstring::const_char_iterator end = mIter; |
|
80 if (mIter != mEnd) { |
|
81 ++mIter; |
|
82 } |
|
83 |
|
84 return Substring(begin, end); |
|
85 } |
|
86 |
|
87 private: |
|
88 nsCSubstring::const_char_iterator mIter, mEnd; |
|
89 }; |
|
90 |
|
91 } // namespace mozilla |
|
92 |
|
93 #endif /* mozilla_CharTokenizer_h */ |