1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-23 08:15:30 +03:00

Added lightweight string handling from iNav

This commit is contained in:
Martin Budden 2017-09-20 10:48:18 +01:00
parent 4f2bea6a84
commit c5ce6d6d35
2 changed files with 102 additions and 0 deletions

74
src/main/common/string_light.c Executable file
View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#include <limits.h>
#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;
}

28
src/main/common/string_light.h Executable file
View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#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);