Skip to content

Commit 5a57f5e

Browse files
committed
Replace token count,len with string_view, closes #517
1 parent 0a13eae commit 5a57f5e

File tree

4 files changed

+19
-25
lines changed

4 files changed

+19
-25
lines changed

regression-tests/test-results/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
cppfront compiler v0.2.1 Build 8618:1005
2+
cppfront compiler v0.2.1 Build 8618:1139
33
Copyright(c) Herb Sutter All rights reserved
44

55
SPDX-License-Identifier: CC-BY-NC-ND-4.0

source/build.info

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"8618:1005"
1+
"8618:1139"

source/cppfront.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6028,8 +6028,7 @@ auto main(
60286028
exit_status = EXIT_FAILURE;
60296029
}
60306030

6031-
// In any case, emit the debug information (during early development this is
6032-
// on by default, later we'll flip the switch to turn it on instead of off)
6031+
// And, if requested, the debug information
60336032
if (enable_debug_output_files) {
60346033
c.debug_print();
60356034
}

source/lex.h

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,9 @@ class token
226226
source_position pos,
227227
lexeme type
228228
)
229-
: start {start}
230-
, count {int16_t(count)}
231-
, pos {pos }
232-
, lex_type{type }
229+
: sv {start, unsafe_narrow<ulong>(count)}
230+
, pos {pos}
231+
, lex_type{type}
233232
{
234233
}
235234

@@ -238,18 +237,17 @@ class token
238237
source_position pos,
239238
lexeme type
240239
)
241-
: start {sz}
242-
, count {__as<int16_t>(std::strlen(sz))}
243-
, pos {pos }
244-
, lex_type{type }
240+
: sv {sz}
241+
, pos {pos}
242+
, lex_type{type}
245243
{
246244
}
247245

248246
auto as_string_view() const
249247
-> std::string_view
250248
{
251-
assert (start);
252-
return {start, static_cast<unsigned>(count)};
249+
assert (sv.data());
250+
return sv;
253251
}
254252

255253
operator std::string_view() const
@@ -272,7 +270,7 @@ class token
272270
auto to_string( bool text_only = false ) const
273271
-> std::string
274272
{
275-
auto text = std::string{start, static_cast<unsigned>(count)};
273+
auto text = std::string{sv};
276274
if (text_only) {
277275
return text;
278276
}
@@ -294,13 +292,13 @@ class token
294292
pos.colno += offset;
295293
}
296294

297-
auto position() const -> source_position { return pos; }
295+
auto position() const -> source_position { return pos; }
298296

299-
auto length () const -> int { return count; }
297+
auto length () const -> int { return sv.size(); }
300298

301-
auto type () const -> lexeme { return lex_type; }
299+
auto type () const -> lexeme { return lex_type; }
302300

303-
auto set_type(lexeme l) -> void { lex_type = l; }
301+
auto set_type(lexeme l) -> void { lex_type = l; }
304302

305303
auto visit(auto& v, int depth) const
306304
-> void
@@ -309,12 +307,9 @@ class token
309307
}
310308

311309
private:
312-
// Store (char*,count) because it's smaller than a string_view
313-
//
314-
char const* start;
315-
int16_t count;
316-
source_position pos;
317-
lexeme lex_type;
310+
std::string_view sv;
311+
source_position pos;
312+
lexeme lex_type;
318313
};
319314

320315
static_assert (CHAR_BIT == 8);

0 commit comments

Comments
 (0)