| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/tools/balsa/split.h" | 5 #include "net/tools/balsa/split.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/strings/string_piece.h" | 11 #include "base/strings/string_piece.h" |
| 12 | 12 |
| 13 namespace net { | 13 namespace net { |
| 14 | 14 |
| 15 // Yea, this could be done with less code duplication using | 15 // Yea, this could be done with less code duplication using |
| 16 // template magic, I know. | 16 // template magic, I know. |
| 17 void SplitStringPieceToVector(const base::StringPiece& full, | 17 void SplitStringPieceToVector(const base::StringPiece& full, |
| 18 const char* delim, | 18 const char* delim, |
| 19 std::vector<base::StringPiece>* vec, | 19 std::vector<base::StringPiece>* vec, |
| 20 bool omit_empty_strings) { | 20 bool omit_empty_strings) { |
| 21 vec->clear(); | 21 vec->clear(); |
| 22 if (full.empty() || delim[0] == '\0') | 22 if (full.empty() || delim[0] == '\0') |
| 23 return; | 23 return; |
| 24 | 24 |
| 25 if (delim[1] == '\0') { | 25 if (delim[1] == '\0') { |
| 26 base::StringPiece::const_iterator s = full.begin(); | 26 base::StringPiece::const_iterator s = full.begin(); |
| 27 base::StringPiece::const_iterator e = s; | 27 base::StringPiece::const_iterator e = s; |
| 28 for (;e != full.end(); ++e) { | 28 for (; e != full.end(); ++e) { |
| 29 if (*e == delim[0]) { | 29 if (*e == delim[0]) { |
| 30 if (e != s || !omit_empty_strings) { | 30 if (e != s || !omit_empty_strings) { |
| 31 vec->push_back(base::StringPiece(s, e - s)); | 31 vec->push_back(base::StringPiece(s, e - s)); |
| 32 } | 32 } |
| 33 s = e; | 33 s = e; |
| 34 ++s; | 34 ++s; |
| 35 } | 35 } |
| 36 } | 36 } |
| 37 if (s != e) { | 37 if (s != e) { |
| 38 --e; | 38 --e; |
| 39 if (e != s || !omit_empty_strings) { | 39 if (e != s || !omit_empty_strings) { |
| 40 vec->push_back(base::StringPiece(s, e - s)); | 40 vec->push_back(base::StringPiece(s, e - s)); |
| 41 } | 41 } |
| 42 } | 42 } |
| 43 } else { | 43 } else { |
| 44 base::StringPiece::const_iterator s = full.begin(); | 44 base::StringPiece::const_iterator s = full.begin(); |
| 45 base::StringPiece::const_iterator e = s; | 45 base::StringPiece::const_iterator e = s; |
| 46 for (;e != full.end(); ++e) { | 46 for (; e != full.end(); ++e) { |
| 47 bool one_matched = false; | 47 bool one_matched = false; |
| 48 for (const char *d = delim; *d != '\0'; ++d) { | 48 for (const char* d = delim; *d != '\0'; ++d) { |
| 49 if (*d == *e) { | 49 if (*d == *e) { |
| 50 one_matched = true; | 50 one_matched = true; |
| 51 break; | 51 break; |
| 52 } | 52 } |
| 53 } | 53 } |
| 54 if (one_matched) { | 54 if (one_matched) { |
| 55 if (e != s || !omit_empty_strings) { | 55 if (e != s || !omit_empty_strings) { |
| 56 vec->push_back(base::StringPiece(s, e - s)); | 56 vec->push_back(base::StringPiece(s, e - s)); |
| 57 } | 57 } |
| 58 s = e; | 58 s = e; |
| 59 ++s; | 59 ++s; |
| 60 } | 60 } |
| 61 } | 61 } |
| 62 if (s != e) { | 62 if (s != e) { |
| 63 --e; | 63 --e; |
| 64 if (e != s || !omit_empty_strings) { | 64 if (e != s || !omit_empty_strings) { |
| 65 vec->push_back(base::StringPiece(s, e - s)); | 65 vec->push_back(base::StringPiece(s, e - s)); |
| 66 } | 66 } |
| 67 } | 67 } |
| 68 } | 68 } |
| 69 } | 69 } |
| 70 | 70 |
| 71 } // namespace net | 71 } // namespace net |
| 72 | |
| OLD | NEW |