Skip to content

Commit

Permalink
Replace token count,len with string_view, closes #517
Browse files Browse the repository at this point in the history
  • Loading branch information
hsutter committed Jun 18, 2023
1 parent 0a13eae commit 5a57f5e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 25 deletions.
2 changes: 1 addition & 1 deletion regression-tests/test-results/version
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

cppfront compiler v0.2.1 Build 8618:1005
cppfront compiler v0.2.1 Build 8618:1139
Copyright(c) Herb Sutter All rights reserved

SPDX-License-Identifier: CC-BY-NC-ND-4.0
Expand Down
2 changes: 1 addition & 1 deletion source/build.info
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"8618:1005"
"8618:1139"
3 changes: 1 addition & 2 deletions source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6028,8 +6028,7 @@ auto main(
exit_status = EXIT_FAILURE;
}

// In any case, emit the debug information (during early development this is
// on by default, later we'll flip the switch to turn it on instead of off)
// And, if requested, the debug information
if (enable_debug_output_files) {
c.debug_print();
}
Expand Down
37 changes: 16 additions & 21 deletions source/lex.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,9 @@ class token
source_position pos,
lexeme type
)
: start {start}
, count {int16_t(count)}
, pos {pos }
, lex_type{type }
: sv {start, unsafe_narrow<ulong>(count)}
, pos {pos}
, lex_type{type}
{
}

Expand All @@ -238,18 +237,17 @@ class token
source_position pos,
lexeme type
)
: start {sz}
, count {__as<int16_t>(std::strlen(sz))}
, pos {pos }
, lex_type{type }
: sv {sz}
, pos {pos}
, lex_type{type}
{
}

auto as_string_view() const
-> std::string_view
{
assert (start);
return {start, static_cast<unsigned>(count)};
assert (sv.data());
return sv;
}

operator std::string_view() const
Expand All @@ -272,7 +270,7 @@ class token
auto to_string( bool text_only = false ) const
-> std::string
{
auto text = std::string{start, static_cast<unsigned>(count)};
auto text = std::string{sv};
if (text_only) {
return text;
}
Expand All @@ -294,13 +292,13 @@ class token
pos.colno += offset;
}

auto position() const -> source_position { return pos; }
auto position() const -> source_position { return pos; }

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

auto type () const -> lexeme { return lex_type; }
auto type () const -> lexeme { return lex_type; }

auto set_type(lexeme l) -> void { lex_type = l; }
auto set_type(lexeme l) -> void { lex_type = l; }

auto visit(auto& v, int depth) const
-> void
Expand All @@ -309,12 +307,9 @@ class token
}

private:
// Store (char*,count) because it's smaller than a string_view
//
char const* start;
int16_t count;
source_position pos;
lexeme lex_type;
std::string_view sv;
source_position pos;
lexeme lex_type;
};

static_assert (CHAR_BIT == 8);
Expand Down

0 comments on commit 5a57f5e

Please sign in to comment.