Skip to content

Commit

Permalink
support: Define substr in terms of length rather than end index
Browse files Browse the repository at this point in the history
This makes it more consistent with both Span and the C++ standard
library.
  • Loading branch information
yeetari committed Mar 4, 2024
1 parent 9ee543b commit 30a654e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
14 changes: 7 additions & 7 deletions engine/include/vull/support/string_view.hh
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ public:
using Span::Span;
constexpr StringView(const char *c_string) : Span(c_string, __builtin_strlen(c_string)) {}

constexpr StringView substr(size_t begin) const;
constexpr StringView substr(size_t begin, size_t end) const;
constexpr StringView substr(size_t offset) const;
constexpr StringView substr(size_t offset, size_t length) const;
constexpr bool operator==(StringView other) const;
constexpr size_t length() const { return size(); }
};

constexpr StringView StringView::substr(size_t begin) const {
return substr(begin, length());
constexpr StringView StringView::substr(size_t offset) const {
return substr(offset, length() - offset);
}

constexpr StringView StringView::substr(size_t begin, size_t end) const {
VULL_ASSERT(begin + (end - begin) <= size());
return {data() + begin, end - begin};
constexpr StringView StringView::substr(size_t offset, size_t length) const {
VULL_ASSERT(offset + length <= size());
return {data() + offset, length};
}

constexpr bool StringView::operator==(StringView other) const {
Expand Down
8 changes: 4 additions & 4 deletions engine/sources/json/lexer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Token Lexer::next_token() {
if (m_head > m_source.length()) {
return Token(TokenKind::Invalid);
}
return Token(m_source.substr(begin + 1, m_head - 1));
return Token(m_source.substr(begin + 1, m_head - begin - 2));
}

if (ch == '-') {
Expand All @@ -49,15 +49,15 @@ Token Lexer::next_token() {
return Token(static_cast<int64_t>(number.get<uint64_t>()));
}

if (ch == 'n' && m_head + 3 <= m_source.length() && m_source.substr(m_head, m_head + 3) == "ull") {
if (ch == 'n' && m_head + 3 <= m_source.length() && m_source.substr(m_head, 3) == "ull") {
m_head += 3;
return Token(TokenKind::Null);
}
if (ch == 't' && m_head + 3 <= m_source.length() && m_source.substr(m_head, m_head + 3) == "rue") {
if (ch == 't' && m_head + 3 <= m_source.length() && m_source.substr(m_head, 3) == "rue") {
m_head += 3;
return Token(TokenKind::True);
}
if (ch == 'f' && m_head + 4 <= m_source.length() && m_source.substr(m_head, m_head + 4) == "alse") {
if (ch == 'f' && m_head + 4 <= m_source.length() && m_source.substr(m_head, 4) == "alse") {
m_head += 4;
return Token(TokenKind::False);
}
Expand Down
4 changes: 2 additions & 2 deletions engine/sources/script/lexer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Token Lexer::next_token() {
while (is_ident(m_source[m_head]) || is_digit(m_source[m_head])) {
m_head++;
}
auto string = m_source.view().substr(position, m_head);
auto string = m_source.view().substr(position, m_head - position);
// TODO: Perfect hashing (http://0x80.pl/notesen/2023-04-30-lookup-in-strings.html) or trie switch.
if (string == "elif") {
return {TokenKind::KW_elif, position, m_line};
Expand Down Expand Up @@ -124,7 +124,7 @@ SourcePosition Lexer::recover_position(const Token &token) const {
line_end++;
}

const auto line_view = m_source.view().substr(line_head, line_end);
const auto line_view = m_source.view().substr(line_head, line_end - line_head);
return {m_file_name, line_view, token.line(), token.position() - line_head + 1};
}

Expand Down
4 changes: 2 additions & 2 deletions engine/sources/shaderc/lexer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Token Lexer::next_token(bool in_comment) {
while (is_ident(m_source[m_head]) || is_digit(m_source[m_head])) {
m_head++;
}
auto string = m_source.view().substr(position, m_head);
auto string = m_source.view().substr(position, m_head - position);
if (string == "fn") {
return MAKE_TOKEN(TokenKind::KW_fn);
}
Expand Down Expand Up @@ -131,7 +131,7 @@ SourcePosition Lexer::recover_position(const Token &token) const {
line_end++;
}

const auto line_view = m_source.view().substr(line_head, line_end);
const auto line_view = m_source.view().substr(line_head, line_end - line_head);
return {m_file_name, line_view, token.line(), token.position() - line_head + 1};
}

Expand Down

0 comments on commit 30a654e

Please sign in to comment.