Skip to content

Commit

Permalink
[cpp/qsn] Add rooting and 100% test coverage -- found bug!
Browse files Browse the repository at this point in the history
unsigned char vs. char!  We might want to change the definition of Str.
  • Loading branch information
Andy C committed Nov 20, 2022
1 parent 38aad5c commit 6343552
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
15 changes: 12 additions & 3 deletions cpp/qsn.h
Expand Up @@ -8,16 +8,25 @@
namespace qsn {

inline bool IsUnprintableLow(Str* ch) {
NO_ROOTS_FRAME(FUNC_NAME); // no allocations

assert(len(ch) == 1);
return ch->data_[0] < ' ';
}

inline bool IsUnprintableHigh(Str* ch) {
NO_ROOTS_FRAME(FUNC_NAME); // no allocations

assert(len(ch) == 1);
return ch->data_[0] >= 0x7f;
// 255 should not be -1!
// log("ch->data_[0] %d", ch->data_[0]);
unsigned char c = static_cast<unsigned char>(ch->data_[0]);
return c >= 0x7f;
}

inline bool IsPlainChar(Str* ch) {
NO_ROOTS_FRAME(FUNC_NAME); // no allocations

assert(len(ch) == 1);
uint8_t c = ch->data_[0];
switch (c) {
Expand All @@ -31,7 +40,7 @@ inline bool IsPlainChar(Str* ch) {
}

inline Str* XEscape(Str* ch) {
NO_ROOTS_FRAME(FUNC_NAME); // NewStr does it
NO_ROOTS_FRAME(FUNC_NAME); // skip to NewStr

assert(len(ch) == 1);
Str* result = NewStr(4);
Expand All @@ -40,7 +49,7 @@ inline Str* XEscape(Str* ch) {
}

inline Str* UEscape(int codepoint) {
NO_ROOTS_FRAME(FUNC_NAME); // OverAllocatedStr does it
NO_ROOTS_FRAME(FUNC_NAME); // skip to OverAllocatedStr

// maximum length:
// 3 for \u{
Expand Down
10 changes: 10 additions & 0 deletions cpp/qsn_test.cc
Expand Up @@ -12,6 +12,16 @@ TEST qsn_test() {
log("UEScape %s", u->data_);
ASSERT(str_equals(u, StrFromC("\\u{61}")));

ASSERT(qsn::IsUnprintableLow(StrFromC("\x01")));
ASSERT(!qsn::IsUnprintableLow(StrFromC(" ")));

ASSERT(qsn::IsUnprintableHigh(StrFromC("\xff")));
ASSERT(!qsn::IsUnprintableHigh(StrFromC("\x01")));

ASSERT(qsn::IsPlainChar(StrFromC("a")));
ASSERT(qsn::IsPlainChar(StrFromC("-")));
ASSERT(!qsn::IsPlainChar(StrFromC(" ")));

PASS();
}

Expand Down

0 comments on commit 6343552

Please sign in to comment.