From c5ce6d6d35737e0849ff8af67e4d7475d756cdb4 Mon Sep 17 00:00:00 2001 From: Martin Budden Date: Wed, 20 Sep 2017 10:48:18 +0100 Subject: [PATCH] Added lightweight string handling from iNav --- src/main/common/string_light.c | 74 ++++++++++++++++++++++++++++++++++ src/main/common/string_light.h | 28 +++++++++++++ 2 files changed, 102 insertions(+) create mode 100755 src/main/common/string_light.c create mode 100755 src/main/common/string_light.h diff --git a/src/main/common/string_light.c b/src/main/common/string_light.c new file mode 100755 index 0000000000..1546f733c4 --- /dev/null +++ b/src/main/common/string_light.c @@ -0,0 +1,74 @@ +/* + * This file is part of Cleanflight. + * + * Cleanflight is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Cleanflight is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Cleanflight. If not, see . + */ + +#include + +#include "string_light.h" +#include "typeconversion.h" + +int sl_isalnum(int c) +{ + return sl_isdigit(c) || sl_isupper(c) || sl_islower(c); +} + +int sl_isdigit(int c) +{ + return (c >= '0' && c <= '9'); +} + +int sl_isupper(int c) +{ + return (c >= 'A' && c <= 'Z'); +} + +int sl_islower(int c) +{ + return (c >= 'a' && c <= 'z'); +} + +int sl_tolower(int c) +{ + return sl_isupper(c) ? (c) - 'A' + 'a' : c; +} + +int sl_toupper(int c) +{ + return sl_islower(c) ? (c) - 'a' + 'A' : c; +} + +int sl_strcasecmp(const char * s1, const char * s2) +{ + return sl_strncasecmp(s1, s2, INT_MAX); +} + +int sl_strncasecmp(const char * s1, const char * s2, int n) +{ + const unsigned char * ucs1 = (const unsigned char *) s1; + const unsigned char * ucs2 = (const unsigned char *) s2; + + int d = 0; + + for ( ; n != 0; n--) { + const int c1 = sl_tolower(*ucs1++); + const int c2 = sl_tolower(*ucs2++); + if (((d = c1 - c2) != 0) || (c2 == '\0')) { + break; + } + } + + return d; +} diff --git a/src/main/common/string_light.h b/src/main/common/string_light.h new file mode 100755 index 0000000000..005a6c6b12 --- /dev/null +++ b/src/main/common/string_light.h @@ -0,0 +1,28 @@ +/* + * This file is part of Cleanflight. + * + * Cleanflight is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Cleanflight is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Cleanflight. If not, see . + */ + +#pragma once + +int sl_isalnum(int c); +int sl_isdigit(int c); +int sl_isupper(int c); +int sl_islower(int c); +int sl_tolower(int c); +int sl_toupper(int c); + +int sl_strcasecmp(const char * s1, const char * s2); +int sl_strncasecmp(const char * s1, const char * s2, int n);