diff options
| author | Ben Winston | 2024-05-31 23:04:13 -0400 |
|---|---|---|
| committer | Ben Winston | 2024-05-31 23:04:13 -0400 |
| commit | 5b43f3caaeb35504a8f65eb4f20b78c9118d9e53 (patch) | |
| tree | 8f3431da1b37dfde2aac94a33376d128f4adbd1c | |
| parent | 30175e8f0f732ae081033351730a0363378c1710 (diff) | |
add leading +/, for greater or less than
| -rw-r--r-- | booki.c | 33 |
1 files changed, 28 insertions, 5 deletions
@@ -462,15 +462,38 @@ bool match_string(const char* pattern, const struct es text) { return valid; } -bool match_int(const char* pattern, const int candidate) { - // need to parse the pattern to an int +bool match_int(char* pattern, int candidate) { + // we don't want to match the zero value, unless the pattern is also 0 + if (candidate == 0 && strcmp(pattern, "0") != 0) + return false; + + // check for leading signs + char* current_pos = pattern; + bool lt = false; + bool gt = false; + if (*current_pos == '+') { + gt = true; + current_pos++; + } else if (*current_pos == ',') { + lt = true; + current_pos++; + } + + // parse the string to an int char* endptr; - long ret = strtol(pattern, &endptr, 10); + long ret = strtol(current_pos, &endptr, 10); if (*endptr != '\0') { - printf("couldn't parse int!\n"); + printf("couldn't parse pattern as int: '%s'\n", pattern); return false; } - return ret == candidate; + + // do compares + if (lt) + return candidate < ret; + else if (gt) + return candidate > ret; + else + return ret == candidate; } static struct option search_options[] = { |
