diff --git a/settings.cpp b/settings.cpp index 1bad69f..425b3d4 100644 --- a/settings.cpp +++ b/settings.cpp @@ -5,17 +5,7 @@ #include "settings.h" #include - -bool is_whitespace(char const c) -{ - return c == ' ' || c == '\t'; -} - -bool is_printable(char const c) -{ - //cf. ASCII table - return c == '\n' || (c >= 0x20 && c <= 0x7E); -} +#include "string_helper.h" Settings::Settings() { diff --git a/string_helper.cpp b/string_helper.cpp new file mode 100644 index 0000000..cff34c2 --- /dev/null +++ b/string_helper.cpp @@ -0,0 +1,47 @@ +/** + * @file string.cpp + * @author n0m1s + */ +#include "./string.h" + +bool string_starts_by(char const* hay, char const* needle) +{ + if(needle == nullptr) + return true; + if(hay == nullptr) + return false; + + for(unsigned int i = 0; needle[i] != '\0'; ++i) + { + if(hay[i] == '\0') + return false; + if(hay[i] != needle[i]) + return false; + } + + return true; +} + +bool string_equals(char const* a, char const* b) +{ + if(a == nullptr) return b == nullptr; + if(b == nullptr) return false; + + unsigned int i = 0; + for(; a[i] != '\0' && b[i] != '\0'; ++i) + if(a[i] != b[i]) return false; + + //either a or b is finished, so we check that both are + return a[i] == b[i]; +} + +char* string_copy(char const* s) +{ + unsigned int const len = strlen(s)+1; + + char * ret = new char[len]; + for(unsigned int i = 0; i < len; ++i) + ret[i] = s[i]; + + return ret; +} diff --git a/string_helper.h b/string_helper.h new file mode 100644 index 0000000..3ae2c40 --- /dev/null +++ b/string_helper.h @@ -0,0 +1,30 @@ +#ifndef STRING_HELPER_H +#define STRING_HELPER_H + +inline bool is_whitespace(char const c) +{ + return c == ' ' || c == '\t'; +} + +inline bool is_printable(char const c) +{ + //cf. ASCII table + return c == '\n' || (c >= 0x20 && c <= 0x7E); +} + +/** + * @brief checks that if a string (hay) starts by a prefix (needle) + */ +bool string_starts_by(char const* hay, char const* needle); + +/** + * @brief checks that two strings have the same characters (and no others) + */ +bool string_equals(char const* a, char const* b); + +/** + * @brief copies a string + */ +char* string_copy(char const* s); + +#endif //STRING_HELPER_H