1 // Copyright (c) 2009 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 <string.h> 6 7 #include <vector> 8 9 #include "base/string_piece.h" 10 11 namespace net { 12 13 // Yea, this could be done with less code duplication using 14 // template magic, I know. 15 void SplitStringPieceToVector(const base::StringPiece& full, 16 const char* delim, 17 std::vector<base::StringPiece>* vec, 18 bool omit_empty_strings) { 19 vec->clear(); 20 if (full.size() == 0 || delim[0] == '\0') 21 return; 22 23 if (delim[1] == '\0') { 24 base::StringPiece::const_iterator s = full.begin(); 25 base::StringPiece::const_iterator e = s; 26 for (;e != full.end(); ++e) { 27 if (*e == delim[0]) { 28 if (e != s || !omit_empty_strings) { 29 vec->push_back(base::StringPiece(s, e - s)); 30 } 31 s = e; 32 ++s; 33 } 34 } 35 if (s != e) { 36 --e; 37 if (e != s || !omit_empty_strings) { 38 vec->push_back(base::StringPiece(s, e - s)); 39 } 40 } 41 } else { 42 base::StringPiece::const_iterator s = full.begin(); 43 base::StringPiece::const_iterator e = s; 44 for (;e != full.end(); ++e) { 45 bool one_matched = false; 46 for (const char *d = delim; *d != '\0'; ++d) { 47 if (*d == *e) { 48 one_matched = true; 49 break; 50 } 51 } 52 if (one_matched) { 53 if (e != s || !omit_empty_strings) { 54 vec->push_back(base::StringPiece(s, e - s)); 55 } 56 s = e; 57 ++s; 58 } 59 } 60 if (s != e) { 61 --e; 62 if (e != s || !omit_empty_strings) { 63 vec->push_back(base::StringPiece(s, e - s)); 64 } 65 } 66 } 67 } 68 69 } // namespace net 70 71