Skip to content
Snippets Groups Projects
Commit 20e398d3 authored by Michal Vlasák's avatar Michal Vlasák
Browse files

Add string functions

parent b69e4e12
No related branches found
No related tags found
No related merge requests found
......@@ -9,11 +9,21 @@
_Noreturn void
unreachable(char *file, size_t line)
{
fprintf(stderr, "ERROR: unreachable code reached at %s:%zu\n", file,
line);
fprintf(stderr, "ERROR: unreachable code reached at %s:%zu\n", file, line);
exit(EXIT_FAILURE);
}
 
bool str_eq(Str a, Str b)
{
return a.len == b.len && memcmp(a.str, b.str, a.len) == 0;
}
int str_cmp(Str a, Str b)
{
int cmp = memcmp(a.str, b.str, a.len < b.len ? a.len : b.len);
return cmp == 0 ? (a.len > b.len) - (b.len > a.len) : cmp;
}
typedef struct {
const u8 *pos;
const u8 *end;
......
......@@ -36,6 +36,16 @@ typedef struct {
} Str;
#define STR(lit) (Str) { .str = (const u8 *) lit, .len = sizeof(lit) - 1 }
 
// Compare equality of strings, returns true if equal, false otherwise.
bool str_eq(Str a, Str b);
// Lexicographically compare two strings. Returns:
// - 0 if the strings are equal,
// - a negative number if the first string is less than the second
// - a positive number if the first string is greater than the second
int str_cmp(Str a, Str b);
// See https://courses.fit.cvut.cz/NI-RUN/specs/ast.html for details about the
// AST.
 
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment