Browse Source

moved string functions to their own file

master
n0m1s 6 years ago
parent
commit
d29c47b73c
3 changed files with 78 additions and 11 deletions
  1. +1
    -11
      settings.cpp
  2. +47
    -0
      string_helper.cpp
  3. +30
    -0
      string_helper.h

+ 1
- 11
settings.cpp View File

@ -5,17 +5,7 @@
#include "settings.h" #include "settings.h"
#include <SD.h> #include <SD.h>
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() Settings::Settings()
{ {


+ 47
- 0
string_helper.cpp View File

@ -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;
}

+ 30
- 0
string_helper.h View File

@ -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

Loading…
Cancel
Save