Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(280)

Side by Side Diff: src/IceRangeSpec.cpp

Issue 1903553004: Subzero: Allow overriding command-line args from the browser. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Code review changes Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/IceRangeSpec.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 //===- subzero/src/IceRangeSpec.cpp - Include/exclude specification -------===// 1 //===- subzero/src/IceRangeSpec.cpp - Include/exclude specification -------===//
2 // 2 //
3 // The Subzero Code Generator 3 // The Subzero Code Generator
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 /// 9 ///
10 /// \file 10 /// \file
(...skipping 18 matching lines...) Expand all
29 #include <string> 29 #include <string>
30 #include <unordered_set> 30 #include <unordered_set>
31 #include <vector> 31 #include <vector>
32 32
33 namespace Ice { 33 namespace Ice {
34 34
35 bool RangeSpec::HasNames = false; 35 bool RangeSpec::HasNames = false;
36 36
37 namespace { 37 namespace {
38 38
39 /// Helper function to tokenize a string into a vector of string tokens, given a
40 /// single delimiter character. An empty string produces an empty token vector.
41 /// Zero-length tokens are allowed, e.g. ",a,,,b," may tokenize to
42 /// {"","a","","","b",""}.
43 std::vector<std::string> tokenize(const std::string &Spec, char Delimiter) {
44 std::vector<std::string> Tokens;
45 if (!Spec.empty()) {
46 std::string::size_type StartPos = 0;
47 std::string::size_type DelimPos = 0;
48 while (DelimPos != std::string::npos) {
49 DelimPos = Spec.find(Delimiter, StartPos);
50 Tokens.emplace_back(Spec.substr(StartPos, DelimPos - StartPos));
51 StartPos = DelimPos + 1;
52 }
53 }
54 return Tokens;
55 }
56
57 /// Helper function to parse "X" or "X:Y" into First and Last. 39 /// Helper function to parse "X" or "X:Y" into First and Last.
58 /// - "X" is treated as "X:X+1". 40 /// - "X" is treated as "X:X+1".
59 /// - ":Y" is treated as "0:Y". 41 /// - ":Y" is treated as "0:Y".
60 /// - "X:" is treated as "X:inf" 42 /// - "X:" is treated as "X:inf"
61 /// 43 ///
62 /// Behavior is undefined if "X" or "Y" is not a proper number (since std::stoul 44 /// Behavior is undefined if "X" or "Y" is not a proper number (since std::stoul
63 /// throws an exception). 45 /// throws an exception).
64 /// 46 ///
65 /// If the string doesn't contain 1 or 2 ':' delimiters, or X>=Y, 47 /// If the string doesn't contain 1 or 2 ':' delimiters, or X>=Y,
66 /// report_fatal_error is called. 48 /// report_fatal_error is called.
67 void getRange(const std::string &Token, uint32_t *First, uint32_t *Last) { 49 void getRange(const std::string &Token, uint32_t *First, uint32_t *Last) {
68 bool Error = false; 50 bool Error = false;
69 auto Tokens = tokenize(Token, RangeSpec::DELIM_RANGE); 51 auto Tokens = RangeSpec::tokenize(Token, RangeSpec::DELIM_RANGE);
70 if (Tokens.size() == 1) { 52 if (Tokens.size() == 1) {
71 *First = std::stoul(Tokens[0]); 53 *First = std::stoul(Tokens[0]);
72 *Last = *First + 1; 54 *Last = *First + 1;
73 } else if (Tokens.size() == 2) { 55 } else if (Tokens.size() == 2) {
74 *First = Tokens[0].empty() ? 0 : std::stoul(Tokens[0]); 56 *First = Tokens[0].empty() ? 0 : std::stoul(Tokens[0]);
75 *Last = Tokens[1].empty() ? RangeSpec::RangeMax : std::stoul(Tokens[1]); 57 *Last = Tokens[1].empty() ? RangeSpec::RangeMax : std::stoul(Tokens[1]);
76 } else { 58 } else {
77 Error = true; 59 Error = true;
78 } 60 }
79 if (*First >= *Last) { 61 if (*First >= *Last) {
(...skipping 25 matching lines...) Expand all
105 D->Numbers.set(First, Last); 87 D->Numbers.set(First, Last);
106 } 88 }
107 } else { 89 } else {
108 // Otherwise treat it as a single name. 90 // Otherwise treat it as a single name.
109 D->Names.insert(Token); 91 D->Names.insert(Token);
110 } 92 }
111 } 93 }
112 94
113 } // end of anonymous namespace 95 } // end of anonymous namespace
114 96
97 std::vector<std::string> RangeSpec::tokenize(const std::string &Spec,
98 char Delimiter) {
99 std::vector<std::string> Tokens;
100 if (!Spec.empty()) {
101 std::string::size_type StartPos = 0;
102 std::string::size_type DelimPos = 0;
103 while (DelimPos != std::string::npos) {
104 DelimPos = Spec.find(Delimiter, StartPos);
105 Tokens.emplace_back(Spec.substr(StartPos, DelimPos - StartPos));
106 StartPos = DelimPos + 1;
107 }
108 }
109 return Tokens;
110 }
111
115 /// Initialize the RangeSpec with the given string. Calling init multiple times 112 /// Initialize the RangeSpec with the given string. Calling init multiple times
116 /// (e.g. init("A");init("B");) is equivalent to init("A,B"); . 113 /// (e.g. init("A");init("B");) is equivalent to init("A,B"); .
117 void RangeSpec::init(const std::string &Spec) { 114 void RangeSpec::init(const std::string &Spec) {
118 auto Tokens = tokenize(Spec, DELIM_LIST); 115 auto Tokens = tokenize(Spec, DELIM_LIST);
119 for (const auto &Token : Tokens) { 116 for (const auto &Token : Tokens) {
120 if (Token[0] == '-') { 117 if (Token[0] == '-') {
121 exclude(Token.substr(1)); 118 exclude(Token.substr(1));
122 } else { 119 } else {
123 include(Token); 120 include(Token);
124 } 121 }
(...skipping 27 matching lines...) Expand all
152 149
153 // Otherwise use the default decision. 150 // Otherwise use the default decision.
154 return Excludes.IsExplicit && !Includes.IsExplicit; 151 return Excludes.IsExplicit && !Includes.IsExplicit;
155 } 152 }
156 153
157 void RangeSpec::include(const std::string &Token) { record(Token, &Includes); } 154 void RangeSpec::include(const std::string &Token) { record(Token, &Includes); }
158 155
159 void RangeSpec::exclude(const std::string &Token) { record(Token, &Excludes); } 156 void RangeSpec::exclude(const std::string &Token) { record(Token, &Excludes); }
160 157
161 } // end of namespace Ice 158 } // end of namespace Ice
OLDNEW
« no previous file with comments | « src/IceRangeSpec.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698