blob: 2130a0090f47067d606cdff540bef70f3167752a [file] [log] [blame]
aimitakeshid074e302010-07-29 10:12:27 +09001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "StringTokenizer.h"
18
19using namespace android;
20
21StringTokenizer::StringTokenizer(const String8& string, const String8& delimiter) {
22 splitString(string, delimiter);
23}
24
25void StringTokenizer::splitString(const String8& string, const String8& delimiter) {
26 for (unsigned int i = 0; i < string.length(); i++) {
27 unsigned int position = string.find(delimiter.string(), i);
28 if (string.length() != position) {
29 String8 token(string.string()+i, position-i);
30 if (token.length()) {
31 mStringTokenizerVector.push(token);
32 i = position + delimiter.length() - 1;
33 }
34 } else {
35 mStringTokenizerVector.push(String8(string.string()+i, string.length()-i));
36 break;
37 }
38 }
39}
40
41StringTokenizer::Iterator StringTokenizer::iterator() {
42 return Iterator(this);
43}
44
45StringTokenizer::Iterator::Iterator(const StringTokenizer::Iterator& iterator) :
46 mStringTokenizer(iterator.mStringTokenizer),
47 mIndex(iterator.mIndex) {
aimitakeshid074e302010-07-29 10:12:27 +090048}
49
50StringTokenizer::Iterator& StringTokenizer::Iterator::operator=(
51 const StringTokenizer::Iterator& iterator) {
aimitakeshid074e302010-07-29 10:12:27 +090052 mStringTokenizer = iterator.mStringTokenizer;
53 mIndex = iterator.mIndex;
54 return *this;
55}
56
57bool StringTokenizer::Iterator::hasNext() {
58 return mIndex < mStringTokenizer->mStringTokenizerVector.size();
59}
60
61String8& StringTokenizer::Iterator::next() {
62 String8& value = mStringTokenizer->mStringTokenizerVector.editItemAt(mIndex);
63 mIndex++;
64 return value;
65}
66