summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Wilgus <wilgus.william@gmail.com>2022-11-13 00:43:43 -0500
committerWilliam Wilgus <me.theuser@yahoo.com>2022-11-13 01:14:49 -0500
commita634557a881b59b8be1dc156f9822c6d20bd8741 (patch)
treefd953a0ea1d9a02a8d73a15f1aead27b59c5ffaa
parentffe2df2e92cbdeb507a49279a85ac88cac2fbe4f (diff)
downloadrockbox-a634557a881b59b8be1dc156f9822c6d20bd8741.tar.gz
rockbox-a634557a881b59b8be1dc156f9822c6d20bd8741.zip
fix strptokspn, add strcspn, fix splash.c
fix off by 1 error in strptokspn, add strcspn, fix fallout in splash.c Change-Id: I61475d9633fc35db5a8ae30cbe588f69f2f7fabc
-rw-r--r--apps/gui/splash.c11
-rw-r--r--apps/playback.c10
-rw-r--r--firmware/SOURCES2
-rw-r--r--firmware/common/strptokspn.c33
-rw-r--r--firmware/libc/strcspn.c45
5 files changed, 65 insertions, 36 deletions
diff --git a/apps/gui/splash.c b/apps/gui/splash.c
index 65c3ad8c13..d0f1fbb67c 100644
--- a/apps/gui/splash.c
+++ b/apps/gui/splash.c
@@ -73,11 +73,11 @@ static bool splash_internal(struct screen * screen, const char *fmt, va_list ap,
73 if (!next) 73 if (!next)
74 return false; /* nothing to display */ 74 return false; /* nothing to display */
75 75
76 lines[line].len = next_len + 1; 76 lines[line].len = next_len;
77 lines[line].str = next; 77 lines[line].str = next;
78 while (true) 78 while (true)
79 { 79 {
80 w = font_getstringnsize(next, next_len + 1, NULL, NULL, fontnum); 80 w = font_getstringnsize(next, next_len, NULL, NULL, fontnum);
81 if (lastbreak) 81 if (lastbreak)
82 { 82 {
83 len = next - lastbreak; 83 len = next - lastbreak;
@@ -90,18 +90,19 @@ static bool splash_internal(struct screen * screen, const char *fmt, va_list ap,
90 break; /* screen full or out of lines */ 90 break; /* screen full or out of lines */
91 x = 0; 91 x = 0;
92 y += chr_h; 92 y += chr_h;
93 lines[++line].len = next_len + len; 93 lines[++line].len = next_len;
94 lines[line].str = next; 94 lines[line].str = next;
95 } 95 }
96 else 96 else
97 { 97 {
98 /* restore & calculate spacing */ 98 /* restore & calculate spacing */
99 lines[line].len += next_len + len + 1; 99 lines[line].len += next_len + 1;
100 x += next_w; 100 x += next_w;
101 } 101 }
102 } 102 }
103 x += w; 103 x += w;
104 lastbreak = next + next_len + 1; 104
105 lastbreak = next + next_len;
105 lastbrkchr = *lastbreak; 106 lastbrkchr = *lastbreak;
106 107
107 next = strptokspn_r(NULL, matchstr, &next_len, &store); 108 next = strptokspn_r(NULL, matchstr, &next_len, &store);
diff --git a/apps/playback.c b/apps/playback.c
index c425e14baf..7eaa149f2c 100644
--- a/apps/playback.c
+++ b/apps/playback.c
@@ -1483,7 +1483,7 @@ static bool audio_init_codec(struct track_info *track_infop,
1483enum { AUTORESUMABLE_UNKNOWN = 0, AUTORESUMABLE_TRUE, AUTORESUMABLE_FALSE }; 1483enum { AUTORESUMABLE_UNKNOWN = 0, AUTORESUMABLE_TRUE, AUTORESUMABLE_FALSE };
1484static bool autoresumable(struct mp3entry *id3) 1484static bool autoresumable(struct mp3entry *id3)
1485{ 1485{
1486 char *endp, *path; 1486 char *path;
1487 size_t len; 1487 size_t len;
1488 bool is_resumable; 1488 bool is_resumable;
1489 1489
@@ -1501,13 +1501,7 @@ static bool autoresumable(struct mp3entry *id3)
1501 if (*path == ':') /* Skip empty search patterns */ 1501 if (*path == ':') /* Skip empty search patterns */
1502 continue; 1502 continue;
1503 1503
1504 /* FIXME: As soon as strcspn or strchrnul are made available in 1504 len = strcspn(path, ":");
1505 the core, the following can be made more efficient. */
1506 endp = strchr(path, ':');
1507 if (endp)
1508 len = endp - path;
1509 else
1510 len = strlen(path);
1511 1505
1512 /* Note: At this point, len is always > 0 */ 1506 /* Note: At this point, len is always > 0 */
1513 1507
diff --git a/firmware/SOURCES b/firmware/SOURCES
index 4aa7c38daf..76d6cee921 100644
--- a/firmware/SOURCES
+++ b/firmware/SOURCES
@@ -271,7 +271,7 @@ libc/strcat.c
271libc/strchr.c 271libc/strchr.c
272libc/strcmp.c 272libc/strcmp.c
273libc/strcpy.c 273libc/strcpy.c
274 274libc/strcspn.c
275libc/strncmp.c 275libc/strncmp.c
276libc/strrchr.c 276libc/strrchr.c
277libc/strstr.c 277libc/strstr.c
diff --git a/firmware/common/strptokspn.c b/firmware/common/strptokspn.c
index f4b92c0712..16aafc66ef 100644
--- a/firmware/common/strptokspn.c
+++ b/firmware/common/strptokspn.c
@@ -39,40 +39,29 @@
39 * Pointer **end 39 * Pointer **end
40 * 40 *
41 * Note the returned token is NOT NULL terminated by the function as in strtok_r 41 * Note the returned token is NOT NULL terminated by the function as in strtok_r
42 * However the caller can use ret[len+1] = '\0'; to emulate a call to strtok_r 42 * However the caller can use ret[len] = '\0'; to emulate a call to strtok_r
43*/ 43*/
44
45const char *strptokspn_r(const char *ptr, const char *sep, size_t *len, const char **end) 44const char *strptokspn_r(const char *ptr, const char *sep, size_t *len, const char **end)
46{ 45{
47 *len = 0; 46 if (ptr == NULL) /* we got NULL input so then we get last position instead */
48 if (!ptr) 47 {
49 /* we got NULL input so then we get our last position instead */
50 ptr = *end; 48 ptr = *end;
49 }
51 50
52 /* pass all letters that are including in the separator string */ 51 /* pass all letters that are including in the separator string */
53 while (*ptr && strchr(sep, *ptr)) 52 while (*ptr && strchr(sep, *ptr))
54 ++ptr; 53 ++ptr;
55 54
56 if (*ptr) { 55 if (*ptr != '\0')
56 {
57 /* so this is where the next piece of string starts */ 57 /* so this is where the next piece of string starts */
58 const char *start = ptr; 58 const char *start = ptr;
59 59 *len = strcspn(ptr, sep); /* Get span until any sep character in string */
60 /* set the end pointer to the first byte after the start */ 60 *end = ptr + *len;
61 *end = start + 1; 61 if (**end) /* the end is not a null byte */
62
63 /* scan through the string to find where it ends, it ends on a
64 null byte or a character that exists in the separator string */
65 while (**end && !strchr(sep, **end))
66 ++*end; 62 ++*end;
67 *len = (*end - start) - 1; /* this would be the string len if there actually was a NULL */ 63 return start;
68 if (**end) { /* the end is not a null byte */
69 ++*end; /* advance last pointer to beyond the match */
70 }
71
72 return start; /* return the position where the string starts */
73 } 64 }
74
75 /* we ended up on a null byte, there are no more strings to find! */
76 return NULL; 65 return NULL;
77} 66}
78 67
@@ -82,7 +71,7 @@ char * strtok_r(char *ptr, const char *sep, char **end)
82 size_t len; 71 size_t len;
83 char * ret = (char*) strptokspn_r((const char*)ptr, sep, &len, (const char**) end); 72 char * ret = (char*) strptokspn_r((const char*)ptr, sep, &len, (const char**) end);
84 if (ret) 73 if (ret)
85 ret[len + 1] = '\0'; 74 ret[len] = '\0';
86 return ret; 75 return ret;
87} 76}
88#endif 77#endif
diff --git a/firmware/libc/strcspn.c b/firmware/libc/strcspn.c
new file mode 100644
index 0000000000..ee50066ed1
--- /dev/null
+++ b/firmware/libc/strcspn.c
@@ -0,0 +1,45 @@
1/*
2FUNCTION
3 <<strcspn>>---count characters not in string
4INDEX
5 strcspn
6ANSI_SYNOPSIS
7 size_t strcspn(const char *<[s1]>, const char *<[s2]>);
8TRAD_SYNOPSIS
9 size_t strcspn(<[s1]>, <[s2]>)
10 char *<[s1]>;
11 char *<[s2]>;
12DESCRIPTION
13 This function computes the length of the initial part of
14 the string pointed to by <[s1]> which consists entirely of
15 characters <[NOT]> from the string pointed to by <[s2]>
16 (excluding the terminating null character).
17RETURNS
18 <<strcspn>> returns the length of the substring found.
19PORTABILITY
20<<strcspn>> is ANSI C.
21<<strcspn>> requires no supporting OS subroutines.
22 */
23#include <string.h>
24#include "_ansi.h" /* for _DEFUN */
25
26size_t
27_DEFUN (strcspn, (s1, s2),
28 _CONST char *s1 _AND
29 _CONST char *s2)
30{
31 _CONST char *s = s1;
32 _CONST char *c;
33 while (*s1)
34 {
35 for (c = s2; *c; c++)
36 {
37 if (*s1 == *c)
38 break;
39 }
40 if (*c)
41 break;
42 s1++;
43 }
44 return s1 - s;
45}