Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "base/string_split.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/string_util.h"
      9 #include "base/third_party/icu/icu_utf.h"
     10 #include "base/utf_string_conversions.h"
     11 
     12 namespace base {
     13 
     14 template<typename STR>
     15 static void SplitStringT(const STR& str,
     16                          const typename STR::value_type s,
     17                          bool trim_whitespace,
     18                          std::vector<STR>* r) {
     19   size_t last = 0;
     20   size_t i;
     21   size_t c = str.size();
     22   for (i = 0; i <= c; ++i) {
     23     if (i == c || str[i] == s) {
     24       size_t len = i - last;
     25       STR tmp = str.substr(last, len);
     26       if (trim_whitespace) {
     27         STR t_tmp;
     28         TrimWhitespace(tmp, TRIM_ALL, &t_tmp);
     29         r->push_back(t_tmp);
     30       } else {
     31         r->push_back(tmp);
     32       }
     33       last = i + 1;
     34     }
     35   }
     36 }
     37 
     38 void SplitString(const std::wstring& str,
     39                  wchar_t c,
     40                  std::vector<std::wstring>* r) {
     41   SplitStringT(str, c, true, r);
     42 }
     43 
     44 #if !defined(WCHAR_T_IS_UTF16)
     45 void SplitString(const string16& str,
     46                  char16 c,
     47                  std::vector<string16>* r) {
     48   DCHECK(CBU16_IS_SINGLE(c));
     49   SplitStringT(str, c, true, r);
     50 }
     51 #endif
     52 
     53 void SplitString(const std::string& str,
     54                  char c,
     55                  std::vector<std::string>* r) {
     56   DCHECK(c >= 0 && c < 0x7F);
     57   SplitStringT(str, c, true, r);
     58 }
     59 
     60 bool SplitStringIntoKeyValues(
     61     const std::string& line,
     62     char key_value_delimiter,
     63     std::string* key, std::vector<std::string>* values) {
     64   key->clear();
     65   values->clear();
     66 
     67   // Find the key string.
     68   size_t end_key_pos = line.find_first_of(key_value_delimiter);
     69   if (end_key_pos == std::string::npos) {
     70     DVLOG(1) << "cannot parse key from line: " << line;
     71     return false;    // no key
     72   }
     73   key->assign(line, 0, end_key_pos);
     74 
     75   // Find the values string.
     76   std::string remains(line, end_key_pos, line.size() - end_key_pos);
     77   size_t begin_values_pos = remains.find_first_not_of(key_value_delimiter);
     78   if (begin_values_pos == std::string::npos) {
     79     DVLOG(1) << "cannot parse value from line: " << line;
     80     return false;   // no value
     81   }
     82   std::string values_string(remains, begin_values_pos,
     83                             remains.size() - begin_values_pos);
     84 
     85   // Construct the values vector.
     86   values->push_back(values_string);
     87   return true;
     88 }
     89 
     90 bool SplitStringIntoKeyValuePairs(
     91     const std::string& line,
     92     char key_value_delimiter,
     93     char key_value_pair_delimiter,
     94     std::vector<std::pair<std::string, std::string> >* kv_pairs) {
     95   kv_pairs->clear();
     96 
     97   std::vector<std::string> pairs;
     98   SplitString(line, key_value_pair_delimiter, &pairs);
     99 
    100   bool success = true;
    101   for (size_t i = 0; i < pairs.size(); ++i) {
    102     // Empty pair. SplitStringIntoKeyValues is more strict about an empty pair
    103     // line, so continue with the next pair.
    104     if (pairs[i].empty())
    105       continue;
    106 
    107     std::string key;
    108     std::vector<std::string> value;
    109     if (!SplitStringIntoKeyValues(pairs[i],
    110                                   key_value_delimiter,
    111                                   &key, &value)) {
    112       // Don't return here, to allow for keys without associated
    113       // values; just record that our split failed.
    114       success = false;
    115     }
    116     DCHECK_LE(value.size(), 1U);
    117     kv_pairs->push_back(make_pair(key, value.empty()? "" : value[0]));
    118   }
    119   return success;
    120 }
    121 
    122 template <typename STR>
    123 static void SplitStringUsingSubstrT(const STR& str,
    124                                     const STR& s,
    125                                     std::vector<STR>* r) {
    126   typename STR::size_type begin_index = 0;
    127   while (true) {
    128     const typename STR::size_type end_index = str.find(s, begin_index);
    129     if (end_index == STR::npos) {
    130       const STR term = str.substr(begin_index);
    131       STR tmp;
    132       TrimWhitespace(term, TRIM_ALL, &tmp);
    133       r->push_back(tmp);
    134       return;
    135     }
    136     const STR term = str.substr(begin_index, end_index - begin_index);
    137     STR tmp;
    138     TrimWhitespace(term, TRIM_ALL, &tmp);
    139     r->push_back(tmp);
    140     begin_index = end_index + s.size();
    141   }
    142 }
    143 
    144 void SplitStringUsingSubstr(const string16& str,
    145                             const string16& s,
    146                             std::vector<string16>* r) {
    147   SplitStringUsingSubstrT(str, s, r);
    148 }
    149 
    150 void SplitStringUsingSubstr(const std::string& str,
    151                             const std::string& s,
    152                             std::vector<std::string>* r) {
    153   SplitStringUsingSubstrT(str, s, r);
    154 }
    155 
    156 void SplitStringDontTrim(const string16& str,
    157                          char16 c,
    158                          std::vector<string16>* r) {
    159   DCHECK(CBU16_IS_SINGLE(c));
    160   SplitStringT(str, c, false, r);
    161 }
    162 
    163 void SplitStringDontTrim(const std::string& str,
    164                          char c,
    165                          std::vector<std::string>* r) {
    166   DCHECK(IsStringUTF8(str));
    167   DCHECK(c >= 0 && c < 0x7F);
    168   SplitStringT(str, c, false, r);
    169 }
    170 
    171 template<typename STR>
    172 void SplitStringAlongWhitespaceT(const STR& str, std::vector<STR>* result) {
    173   const size_t length = str.length();
    174   if (!length)
    175     return;
    176 
    177   bool last_was_ws = false;
    178   size_t last_non_ws_start = 0;
    179   for (size_t i = 0; i < length; ++i) {
    180     switch (str[i]) {
    181       // HTML 5 defines whitespace as: space, tab, LF, line tab, FF, or CR.
    182       case L' ':
    183       case L'\t':
    184       case L'\xA':
    185       case L'\xB':
    186       case L'\xC':
    187       case L'\xD':
    188         if (!last_was_ws) {
    189           if (i > 0) {
    190             result->push_back(
    191                 str.substr(last_non_ws_start, i - last_non_ws_start));
    192           }
    193           last_was_ws = true;
    194         }
    195         break;
    196 
    197       default:  // Not a space character.
    198         if (last_was_ws) {
    199           last_was_ws = false;
    200           last_non_ws_start = i;
    201         }
    202         break;
    203     }
    204   }
    205   if (!last_was_ws) {
    206     result->push_back(
    207         str.substr(last_non_ws_start, length - last_non_ws_start));
    208   }
    209 }
    210 
    211 void SplitStringAlongWhitespace(const std::wstring& str,
    212                                 std::vector<std::wstring>* result) {
    213   SplitStringAlongWhitespaceT(str, result);
    214 }
    215 
    216 #if !defined(WCHAR_T_IS_UTF16)
    217 void SplitStringAlongWhitespace(const string16& str,
    218                                 std::vector<string16>* result) {
    219   SplitStringAlongWhitespaceT(str, result);
    220 }
    221 #endif
    222 
    223 void SplitStringAlongWhitespace(const std::string& str,
    224                                 std::vector<std::string>* result) {
    225   SplitStringAlongWhitespaceT(str, result);
    226 }
    227 
    228 }  // namespace base
    229