Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
inactive123 committed Jul 6, 2017
1 parent 2e1e334 commit d0dd95f
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 37 deletions.
45 changes: 39 additions & 6 deletions libretro-common/include/string/stdstring.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2010-2015 The RetroArch team
/* Copyright (C) 2010-2017 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (stdstring.h).
Expand All @@ -25,18 +25,49 @@

#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>
#include <string.h>
#include <boolean.h>

#include <retro_common_api.h>
#include <retro_inline.h>

RETRO_BEGIN_DECLS

bool string_is_empty(const char *data);

bool string_is_equal(const char *a, const char *b);

bool string_is_equal_noncase(const char *a, const char *b);
static INLINE bool string_is_empty(const char *data)
{
return (data == NULL) || (*data == '\0');
}

static INLINE bool string_is_equal(const char *a, const char *b)
{
if (!a || !b)
return false;
while(*a && (*a == *b))
a++, b++;
return (*(const unsigned char*)a - *(const unsigned char*)b) == 0;
}

#define string_is_not_equal_fast(a, b, size) (memcmp(a, b, size) != 0)
#define string_is_equal_fast(a, b, size) (memcmp(a, b, size) == 0)

static INLINE bool string_is_equal_noncase(const char *a, const char *b)
{
int result;
const unsigned char *p1 = (const unsigned char*)a;
const unsigned char *p2 = (const unsigned char*)b;

if (!a || !b)
return false;
if (p1 == p2)
return false;

while ((result = tolower (*p1) - tolower (*p2++)) == 0)
if (*p1++ == '\0')
break;

return (result == 0);
}

char *string_to_upper(char *s);

Expand All @@ -56,6 +87,8 @@ char *string_trim_whitespace_right(char *const s);
/* Remove leading and trailing whitespaces */
char *string_trim_whitespace(char *const s);

char *word_wrap(char* buffer, const char *string, int line_width);

RETRO_END_DECLS

#endif
98 changes: 67 additions & 31 deletions libretro-common/string/stdstring.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2010-2016 The RetroArch team
/* Copyright (C) 2010-2017 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (stdstring.c).
Expand All @@ -25,25 +25,6 @@

#include <string/stdstring.h>

bool string_is_empty(const char *data)
{
return (data == NULL) || (*data == '\0');
}

bool string_is_equal(const char *a, const char *b)
{
if (!a || !b)
return false;
return (strcmp(a, b) == 0);
}

bool string_is_equal_noncase(const char *a, const char *b)
{
if (!a || !b)
return false;
return (strcasecmp(a, b) == 0);
}

char *string_to_upper(char *s)
{
char *cs = (char *)s;
Expand All @@ -62,17 +43,15 @@ char *string_to_lower(char *s)

char *string_ucwords(char *s)
{
char *cs = (char *)s;
for ( ; *cs != '\0'; cs++)
{
if (*cs == ' ')
{
*(cs+1) = toupper(*(cs+1));
}
}

s[0] = toupper(s[0]);
return s;
char *cs = (char *)s;
for ( ; *cs != '\0'; cs++)
{
if (*cs == ' ')
*(cs+1) = toupper(*(cs+1));
}

s[0] = toupper(s[0]);
return s;
}

char *string_replace_substring(const char *in,
Expand Down Expand Up @@ -164,3 +143,60 @@ char *string_trim_whitespace(char *const s)

return s;
}

char *word_wrap(char* buffer, const char *string, int line_width)
{
unsigned i = 0;
unsigned len = (unsigned)strlen(string);

while (i < len)
{
unsigned counter;

/* copy string until the end of the line is reached */
for (counter = 1; counter <= (unsigned)line_width; counter++)
{
/* check if end of string reached */
if (i == strlen(string))
{
buffer[i] = 0;
return buffer;
}

buffer[i] = string[i];

/* check for newlines embedded in the original input
* and reset the index */
if (buffer[i] == '\n')
counter = 1;
i++;
}

/* check for whitespace */
if (string[i] == ' ')
{
buffer[i] = '\n';
i++;
}
else
{
int k;

/* check for nearest whitespace back in string */
for (k = i; k > 0; k--)
{
if (string[k] != ' ')
continue;

buffer[k] = '\n';
/* set string index back to character after this one */
i = k + 1;
break;
}
}
}

buffer[i] = 0;

return buffer;
}

0 comments on commit d0dd95f

Please sign in to comment.