aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2024-12-19 17:04:19 -0500
committerClyne Sullivan <clyne@bitgloo.com>2024-12-19 17:04:19 -0500
commitb5ec68178067372c267831d64cf6e515435c9bef (patch)
tree287291d5efd08d22e315643a5e24114e745f0d65
parentc229a831210c0b3c1fb24f9ccfd9e190f0f22a5e (diff)
fix case testing
-rw-r--r--main.cpp7
-rw-r--r--sforth/native_word.hpp2
-rw-r--r--sforth/types.hpp22
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<typename T>
constexpr std::optional<T> from_chars(std::string_view sv, int base = 10)
{
@@ -132,7 +152,7 @@ struct word_base : public word_list
std::optional<const word_base *> 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;
}