From b5ec68178067372c267831d64cf6e515435c9bef Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 19 Dec 2024 17:04:19 -0500 Subject: [PATCH] fix case testing --- main.cpp | 7 +------ sforth/native_word.hpp | 2 +- sforth/types.hpp | 22 +++++++++++++++++++++- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/main.cpp b/main.cpp index be00744..83c3a81 100644 --- a/main.cpp +++ b/main.cpp @@ -67,12 +67,7 @@ bool parse_stream(auto &fth, std::istream& str, bool say_okay) while (str.good()) { std::getline(str, line); if (!line.empty()) { - for (auto& ch : line) { - if (ch >= 'a' && ch <= 'z') - ch = ch - 'a' + 'A'; - } - - if (line == "BYE") + if (sforth::isequal(line, "bye")) return true; try { diff --git a/sforth/native_word.hpp b/sforth/native_word.hpp index 5a98614..a329a72 100644 --- a/sforth/native_word.hpp +++ b/sforth/native_word.hpp @@ -31,7 +31,7 @@ struct native_word : public word_base func body; constexpr const func *get_ct(std::string_view name) const { - if (name == std::string_view{namebuf.data()}) + if (isequal(name, std::string_view{namebuf.data()})) return &body; else if constexpr (Prev != nullptr) return Prev->get_ct(name); diff --git a/sforth/types.hpp b/sforth/types.hpp index 7950664..3cb76ba 100644 --- a/sforth/types.hpp +++ b/sforth/types.hpp @@ -51,6 +51,26 @@ using daddr = __uint128; struct word_base; +constexpr bool isequal(std::string_view a, std::string_view b) +{ + if (a.size() != b.size()) + return false; + + for (unsigned i = 0; i < a.size(); i++) { + auto ac = a[i]; + if (ac >= 'A' && ac <= 'Z') + ac += 32; + auto bc = b[i]; + if (bc >= 'A' && bc <= 'Z') + bc += 32; + + if (ac != bc) + return false; + } + + return true; +} + template constexpr std::optional from_chars(std::string_view sv, int base = 10) { @@ -132,7 +152,7 @@ struct word_base : public word_list std::optional word_list::get(std::string_view sv) const { for (auto lt = next; lt; lt = lt->next) { - if (sv == lt->name()) + if (isequal(sv, lt->name())) return lt; }