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