Skip to content

Commit d0dd95f

Browse files
committed
Update
1 parent 2e1e334 commit d0dd95f

File tree

2 files changed

+106
-37
lines changed

2 files changed

+106
-37
lines changed

libretro-common/include/string/stdstring.h

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (C) 2010-2015 The RetroArch team
1+
/* Copyright (C) 2010-2017 The RetroArch team
22
*
33
* ---------------------------------------------------------------------------------------
44
* The following license statement only applies to this file (stdstring.h).
@@ -25,18 +25,49 @@
2525

2626
#include <stdlib.h>
2727
#include <stddef.h>
28+
#include <ctype.h>
2829
#include <string.h>
2930
#include <boolean.h>
3031

3132
#include <retro_common_api.h>
33+
#include <retro_inline.h>
3234

3335
RETRO_BEGIN_DECLS
3436

35-
bool string_is_empty(const char *data);
36-
37-
bool string_is_equal(const char *a, const char *b);
38-
39-
bool string_is_equal_noncase(const char *a, const char *b);
37+
static INLINE bool string_is_empty(const char *data)
38+
{
39+
return (data == NULL) || (*data == '\0');
40+
}
41+
42+
static INLINE bool string_is_equal(const char *a, const char *b)
43+
{
44+
if (!a || !b)
45+
return false;
46+
while(*a && (*a == *b))
47+
a++, b++;
48+
return (*(const unsigned char*)a - *(const unsigned char*)b) == 0;
49+
}
50+
51+
#define string_is_not_equal_fast(a, b, size) (memcmp(a, b, size) != 0)
52+
#define string_is_equal_fast(a, b, size) (memcmp(a, b, size) == 0)
53+
54+
static INLINE bool string_is_equal_noncase(const char *a, const char *b)
55+
{
56+
int result;
57+
const unsigned char *p1 = (const unsigned char*)a;
58+
const unsigned char *p2 = (const unsigned char*)b;
59+
60+
if (!a || !b)
61+
return false;
62+
if (p1 == p2)
63+
return false;
64+
65+
while ((result = tolower (*p1) - tolower (*p2++)) == 0)
66+
if (*p1++ == '\0')
67+
break;
68+
69+
return (result == 0);
70+
}
4071

4172
char *string_to_upper(char *s);
4273

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

90+
char *word_wrap(char* buffer, const char *string, int line_width);
91+
5992
RETRO_END_DECLS
6093

6194
#endif

libretro-common/string/stdstring.c

Lines changed: 67 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (C) 2010-2016 The RetroArch team
1+
/* Copyright (C) 2010-2017 The RetroArch team
22
*
33
* ---------------------------------------------------------------------------------------
44
* The following license statement only applies to this file (stdstring.c).
@@ -25,25 +25,6 @@
2525

2626
#include <string/stdstring.h>
2727

28-
bool string_is_empty(const char *data)
29-
{
30-
return (data == NULL) || (*data == '\0');
31-
}
32-
33-
bool string_is_equal(const char *a, const char *b)
34-
{
35-
if (!a || !b)
36-
return false;
37-
return (strcmp(a, b) == 0);
38-
}
39-
40-
bool string_is_equal_noncase(const char *a, const char *b)
41-
{
42-
if (!a || !b)
43-
return false;
44-
return (strcasecmp(a, b) == 0);
45-
}
46-
4728
char *string_to_upper(char *s)
4829
{
4930
char *cs = (char *)s;
@@ -62,17 +43,15 @@ char *string_to_lower(char *s)
6243

6344
char *string_ucwords(char *s)
6445
{
65-
char *cs = (char *)s;
66-
for ( ; *cs != '\0'; cs++)
67-
{
68-
if (*cs == ' ')
69-
{
70-
*(cs+1) = toupper(*(cs+1));
71-
}
72-
}
73-
74-
s[0] = toupper(s[0]);
75-
return s;
46+
char *cs = (char *)s;
47+
for ( ; *cs != '\0'; cs++)
48+
{
49+
if (*cs == ' ')
50+
*(cs+1) = toupper(*(cs+1));
51+
}
52+
53+
s[0] = toupper(s[0]);
54+
return s;
7655
}
7756

7857
char *string_replace_substring(const char *in,
@@ -164,3 +143,60 @@ char *string_trim_whitespace(char *const s)
164143

165144
return s;
166145
}
146+
147+
char *word_wrap(char* buffer, const char *string, int line_width)
148+
{
149+
unsigned i = 0;
150+
unsigned len = (unsigned)strlen(string);
151+
152+
while (i < len)
153+
{
154+
unsigned counter;
155+
156+
/* copy string until the end of the line is reached */
157+
for (counter = 1; counter <= (unsigned)line_width; counter++)
158+
{
159+
/* check if end of string reached */
160+
if (i == strlen(string))
161+
{
162+
buffer[i] = 0;
163+
return buffer;
164+
}
165+
166+
buffer[i] = string[i];
167+
168+
/* check for newlines embedded in the original input
169+
* and reset the index */
170+
if (buffer[i] == '\n')
171+
counter = 1;
172+
i++;
173+
}
174+
175+
/* check for whitespace */
176+
if (string[i] == ' ')
177+
{
178+
buffer[i] = '\n';
179+
i++;
180+
}
181+
else
182+
{
183+
int k;
184+
185+
/* check for nearest whitespace back in string */
186+
for (k = i; k > 0; k--)
187+
{
188+
if (string[k] != ' ')
189+
continue;
190+
191+
buffer[k] = '\n';
192+
/* set string index back to character after this one */
193+
i = k + 1;
194+
break;
195+
}
196+
}
197+
}
198+
199+
buffer[i] = 0;
200+
201+
return buffer;
202+
}

0 commit comments

Comments
 (0)