projects
/
open-adventure.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
6e67222
)
Reducing use of dynamic allocation forecloses many errors.
author
Eric S. Raymond
<esr@thyrsus.com>
Mon, 3 Jul 2017 21:53:25 +0000
(17:53 -0400)
committer
Eric S. Raymond
<esr@thyrsus.com>
Mon, 3 Jul 2017 21:53:25 +0000
(17:53 -0400)
misc.c
patch
|
blob
|
history
diff --git
a/misc.c
b/misc.c
index 4fa173d91f4d5b9ba457954cf136e408ef562600..0b00992b6e0b1b2342a93258aa7a4db680bcbf41 100644
(file)
--- a/
misc.c
+++ b/
misc.c
@@
-10,18
+10,6
@@
#include "advent.h"
#include "dungeon.h"
#include "advent.h"
#include "dungeon.h"
-static char* xstrdup(const char* s)
-{
- char* ptr = strdup(s);
- if (ptr == NULL) {
- // LCOV_EXCL_START
- // exclude from coverage analysis because we can't simulate an out of memory error in testing
- fprintf(stderr, "Out of memory!\n");
- exit(EXIT_FAILURE);
- }
- return (ptr);
-}
-
static void* xmalloc(size_t size)
{
void* ptr = malloc(size);
static void* xmalloc(size_t size)
{
void* ptr = malloc(size);
@@
-293,19
+281,24
@@
void echo_input(FILE* destination, const char* input_prompt, const char* input)
free(prompt_and_input);
}
free(prompt_and_input);
}
-int word_count(char* s)
+int word_count(char* s
tr
)
{
{
- char* copy = xstrdup(s);
char delims[] = " \t";
int count = 0;
char delims[] = " \t";
int count = 0;
- char* word;
+ int inblanks = true;
+
+ for (char *s = str; *s; s++)
+ if (inblanks) {
+ if (strchr(delims, *s) == 0) {
+ ++count;
+ inblanks = false;
+ }
+ } else {
+ if (strchr(delims, *s) != 0) {
+ inblanks = true;
+ }
+ }
- word = strtok(copy, delims);
- while (word != NULL) {
- word = strtok(NULL, delims);
- ++count;
- }
- free(copy);
return (count);
}
return (count);
}