Skip to content

Commit 81da743

Browse files
committed
Add a utility function for line tokenizing
1 parent 2acc4c5 commit 81da743

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/util.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ static bool pushline(LacoState* laco, bool isFirstLine) {
100100
return result;
101101
}
102102

103+
static inline void ignore_extra_spaces(char** string_ptr) {
104+
if(*string_ptr == NULL) return;
105+
106+
while(**string_ptr == ' ') {
107+
*string_ptr += 1;
108+
}
109+
}
110+
103111
/* External API */
104112

105113
int laco_load_line(LacoState* laco) {
@@ -170,3 +178,22 @@ int laco_is_match(const char** matches, const char* test_string) {
170178

171179
return false;
172180
}
181+
182+
char** laco_line_to_words(char* line) {
183+
if(line == NULL) return NULL;
184+
185+
char** result = calloc(16, sizeof(char*));
186+
size_t i = 0;
187+
188+
while(1) {
189+
result[i] = strsep(&line, " ");
190+
191+
if(result[i] == NULL) break;
192+
193+
ignore_extra_spaces(&line);
194+
195+
i += 1;
196+
}
197+
198+
return result;
199+
}

src/util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ void laco_report_error(struct LacoState* laco, int status);
3838
*/
3939
int laco_is_match(const char** matches, const char* test_string);
4040

41+
char** laco_line_to_words(char* line);
42+
4143
#endif /* LACO_UTIL_H */

0 commit comments

Comments
 (0)