1 /*
2 * Copyright (C) 2011 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 #include "strUtils.h"
17 
18 using namespace std;
19 
20 
getNextToken(const std::string & str,size_t pos,size_t * last,const std::string & delim)21 std::string getNextToken(const std::string & str, size_t pos, size_t * last, const std::string & delim)
22 {
23     if (str.size() == 0 || pos >= str.size()) return "";
24 
25     pos = str.find_first_not_of(WHITESPACE, pos);
26     if (pos == std::string::npos) return "";
27 
28     *last = str.find_first_of(delim, pos);
29     if (*last == std::string::npos) *last = str.size();
30     std::string retval = str.substr(pos, *last - pos);
31     retval = trim(retval);
32     return retval;
33 }
34 
35 
trim(const string & str)36 std::string trim(const string & str)
37 {
38   string result;
39   string::size_type start = str.find_first_not_of(WHITESPACE, 0);
40   string::size_type end = str.find_last_not_of(WHITESPACE);
41   if (start == string::npos || end == string::npos) {
42     result = string("");
43   } else {
44     result = str.substr(start, end - start + 1);
45   }
46   return result;
47 }
48 
49 
50