diff options
author | John Denker <jsd@av8n.com> | 2012-11-22 16:01:04 -0800 |
---|---|---|
committer | John Denker <jsd@av8n.com> | 2012-11-22 16:01:04 -0800 |
commit | 3c9817f94592907c8c19a8b8f97af87dad3dceaf (patch) | |
tree | 5f2433dbd4d7a0c872fd3400897458b79effac67 /tools/utils.c | |
parent | bf1bf1c43a82ec167ae80f185fe11255cf3a5237 (diff) |
move case-insensitive string stuff to utils.h
Diffstat (limited to 'tools/utils.c')
-rw-r--r-- | tools/utils.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/tools/utils.c b/tools/utils.c index a7c2878..6dbbff4 100644 --- a/tools/utils.c +++ b/tools/utils.c @@ -87,3 +87,31 @@ string purify(const string arg){ } return rslt; } + +// Returns negative if a is less than b in alphabetical order +// returns 0 if they are the same, or positive if a is greater. +// Like perl cmp operator, but ignores case. +int cmp_casefold(const std::string& a, const std::string& b) { + string::const_iterator aa, bb; + aa = a.begin(); + bb = b.begin(); + while (aa != a.end() && bb != b.end()){ + char ca = tolower(*aa++); + char cb = tolower(*bb++); + if (ca != cb) return ca < cb ? -2 : 2; + } + if (aa != a.end()) return 1; // a is longer + if (bb != b.end()) return -1; // b is longer + return 0; +} + +string noCR(const string bar){ + string foo(bar); + int len = foo.length(); + if (len){ + if (foo[len-1] == '\r') { + foo.erase(len-1); + } + } + return foo; +} |