@@ -26,7 +26,7 @@ static std::optional<AlignStyle> translateLocChar(char C) {
26
26
}
27
27
28
28
static bool consumeFieldLayout (StringRef &Spec, AlignStyle &Where,
29
- size_t &Align, char &Pad) {
29
+ unsigned &Align, char &Pad) {
30
30
Where = AlignStyle::Right;
31
31
Align = 0 ;
32
32
Pad = ' ' ;
@@ -60,14 +60,14 @@ static std::optional<ReplacementItem> parseReplacementItem(StringRef Spec) {
60
60
// If the replacement sequence does not start with a non-negative integer,
61
61
// this is an error.
62
62
char Pad = ' ' ;
63
- std:: size_t Align = 0 ;
63
+ unsigned Align = 0 ;
64
64
AlignStyle Where = AlignStyle::Right;
65
65
StringRef Options;
66
- size_t Index = 0 ;
66
+ unsigned Index = 0 ;
67
67
RepString = RepString.trim ();
68
68
if (RepString.consumeInteger (0 , Index)) {
69
69
assert (false && " Invalid replacement sequence index!" );
70
- return ReplacementItem{} ;
70
+ return std::nullopt ;
71
71
}
72
72
RepString = RepString.trim ();
73
73
if (RepString.consume_front (" ," )) {
@@ -83,61 +83,50 @@ static std::optional<ReplacementItem> parseReplacementItem(StringRef Spec) {
83
83
assert (RepString.empty () &&
84
84
" Unexpected characters found in replacement string!" );
85
85
86
- return ReplacementItem{ Spec, Index, Align, Where, Pad, Options} ;
86
+ return ReplacementItem ( Spec, Index, Align, Where, Pad, Options) ;
87
87
}
88
88
89
- static std::pair<ReplacementItem, StringRef>
89
+ static std::pair<std::optional< ReplacementItem> , StringRef>
90
90
splitLiteralAndReplacement (StringRef Fmt) {
91
- while (!Fmt.empty ()) {
92
- // Everything up until the first brace is a literal.
93
- if (Fmt.front () != ' {' ) {
94
- std::size_t BO = Fmt.find_first_of (' {' );
95
- return std::make_pair (ReplacementItem{Fmt.substr (0 , BO)}, Fmt.substr (BO));
96
- }
97
-
98
- StringRef Braces = Fmt.take_while ([](char C) { return C == ' {' ; });
99
- // If there is more than one brace, then some of them are escaped. Treat
100
- // these as replacements.
101
- if (Braces.size () > 1 ) {
102
- size_t NumEscapedBraces = Braces.size () / 2 ;
103
- StringRef Middle = Fmt.take_front (NumEscapedBraces);
104
- StringRef Right = Fmt.drop_front (NumEscapedBraces * 2 );
105
- return std::make_pair (ReplacementItem{Middle}, Right);
106
- }
107
- // An unterminated open brace is undefined. Assert to indicate that this is
108
- // undefined and that we consider it an error. When asserts are disabled,
109
- // build a replacement item with an error message.
110
- std::size_t BC = Fmt.find_first_of (' }' );
111
- if (BC == StringRef::npos) {
112
- assert (
113
- false &&
114
- " Unterminated brace sequence. Escape with {{ for a literal brace." );
115
- return std::make_pair (
116
- ReplacementItem{" Unterminated brace sequence. Escape with {{ for a "
117
- " literal brace." },
118
- StringRef ());
119
- }
91
+ assert (!Fmt.empty ());
92
+ // Everything up until the first brace is a literal.
93
+ if (Fmt.front () != ' {' ) {
94
+ size_t BO = Fmt.find_first_of (' {' );
95
+ return {ReplacementItem{Fmt.substr (0 , BO)}, Fmt.substr (BO)};
96
+ }
120
97
121
- // Even if there is a closing brace, if there is another open brace before
122
- // this closing brace, treat this portion as literal, and try again with the
123
- // next one.
124
- std::size_t BO2 = Fmt.find_first_of (' {' , 1 );
125
- if (BO2 < BC)
126
- return std::make_pair (ReplacementItem{Fmt.substr (0 , BO2)},
127
- Fmt.substr (BO2));
98
+ StringRef Braces = Fmt.take_while ([](char C) { return C == ' {' ; });
99
+ // If there is more than one brace, then some of them are escaped. Treat
100
+ // these as replacements.
101
+ if (Braces.size () > 1 ) {
102
+ size_t NumEscapedBraces = Braces.size () / 2 ;
103
+ StringRef Middle = Fmt.take_front (NumEscapedBraces);
104
+ StringRef Right = Fmt.drop_front (NumEscapedBraces * 2 );
105
+ return {ReplacementItem (Middle), Right};
106
+ }
107
+ // An unterminated open brace is undefined. Assert to indicate that this is
108
+ // undefined and that we consider it an error. When asserts are disabled,
109
+ // build a replacement item with an error message.
110
+ size_t BC = Fmt.find_first_of (' }' );
111
+ if (BC == StringRef::npos) {
112
+ assert (false &&
113
+ " Unterminated brace sequence. Escape with {{ for a literal brace." );
114
+ return {ReplacementItem (" Unterminated brace sequence. Escape with {{ for a "
115
+ " literal brace." ),
116
+ StringRef ()};
117
+ }
128
118
129
- StringRef Spec = Fmt.slice (1 , BC);
130
- StringRef Right = Fmt.substr (BC + 1 );
119
+ // Even if there is a closing brace, if there is another open brace before
120
+ // this closing brace, treat this portion as literal, and try again with the
121
+ // next one.
122
+ size_t BO2 = Fmt.find_first_of (' {' , 1 );
123
+ if (BO2 < BC)
124
+ return {ReplacementItem (Fmt.substr (0 , BO2)), Fmt.substr (BO2)};
131
125
132
- auto RI = parseReplacementItem (Spec);
133
- if (RI)
134
- return std::make_pair (*RI, Right);
126
+ StringRef Spec = Fmt.slice (1 , BC);
127
+ StringRef Right = Fmt.substr (BC + 1 );
135
128
136
- // If there was an error parsing the replacement item, treat it as an
137
- // invalid replacement spec, and just continue.
138
- Fmt = Fmt.drop_front (BC + 1 );
139
- }
140
- return std::make_pair (ReplacementItem{Fmt}, StringRef ());
129
+ return {parseReplacementItem (Spec), Right};
141
130
}
142
131
143
132
#ifndef NDEBUG
@@ -153,17 +142,18 @@ formatv_object_base::parseFormatString(StringRef Fmt, size_t NumArgs,
153
142
154
143
#if ENABLE_VALIDATION
155
144
const StringRef SavedFmtStr = Fmt;
156
- size_t NumExpectedArgs = 0 ;
145
+ unsigned NumExpectedArgs = 0 ;
157
146
#endif
158
147
159
148
while (!Fmt.empty ()) {
160
- ReplacementItem I;
149
+ std::optional< ReplacementItem> I;
161
150
std::tie (I, Fmt) = splitLiteralAndReplacement (Fmt);
162
- if (I.Type != ReplacementType::Empty)
163
- Replacements.push_back (I);
151
+ if (!I)
152
+ continue ;
153
+ Replacements.emplace_back (*I);
164
154
#if ENABLE_VALIDATION
165
- if (I. Type == ReplacementType::Format)
166
- NumExpectedArgs = std::max (NumExpectedArgs, I. Index + 1 );
155
+ if (I-> Type == ReplacementType::Format)
156
+ NumExpectedArgs = std::max (NumExpectedArgs, I-> Index + 1 );
167
157
#endif
168
158
}
169
159
@@ -195,7 +185,7 @@ formatv_object_base::parseFormatString(StringRef Fmt, size_t NumArgs,
195
185
// Find the number of unique indices seen. All replacement indices
196
186
// are < NumExpectedArgs.
197
187
SmallVector<bool > Indices (NumExpectedArgs);
198
- size_t Count = 0 ;
188
+ unsigned Count = 0 ;
199
189
for (const ReplacementItem &I : Replacements) {
200
190
if (I.Type != ReplacementType::Format || Indices[I.Index ])
201
191
continue ;
0 commit comments