- Added string strip methods lstrip and rstrip
- Fixed the tokenizer to chomp whitespace from left and right of tokens - Fixed the tokenizer so it returns reserved symbols not just constants and expressions - Added some tests for the basic tokenizer and parser - Started working on structures to allow the basic interpreter to store lines in memory
This commit is contained in:
56
src/string.c
56
src/string.c
@@ -81,3 +81,59 @@ int strcmp(char *s1, char *s2)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lstrip(char *s1, char *s2, char *strip)
|
||||
{
|
||||
int stripped = 0;
|
||||
char *stripptr = strip;
|
||||
if ( s1 == NULL || s2 == NULL || strip == NULL ) {
|
||||
return 0;
|
||||
}
|
||||
while ( *s1 != 0 ) {
|
||||
if ( stripptr != NULL ) {
|
||||
for ( stripptr = strip; *stripptr != 0; stripptr += 1) {
|
||||
if ( *s1 == *stripptr ) {
|
||||
stripped += 1;
|
||||
goto _lstrip_outer_continue;
|
||||
}
|
||||
}
|
||||
stripptr = NULL;
|
||||
}
|
||||
*s2 = *s1;
|
||||
s2 += 1;
|
||||
_lstrip_outer_continue:
|
||||
s1 += 1;
|
||||
}
|
||||
return stripped;
|
||||
}
|
||||
|
||||
int rstrip(char *s1, char *s2, char *strip)
|
||||
{
|
||||
int stripped = 0;
|
||||
char *stripptr = strip;
|
||||
char *rs1 = s1;
|
||||
if ( s1 == NULL || s2 == NULL || strip == NULL ) {
|
||||
return 0;
|
||||
}
|
||||
rs1 += strlen(s1)-1;
|
||||
while ( rs1 >= s1 ) {
|
||||
for ( stripptr = strip; *stripptr != 0; stripptr += 1) {
|
||||
if ( *rs1 == *stripptr ) {
|
||||
stripped += 1;
|
||||
rs1 -= 1;
|
||||
goto _rstrip_continue;
|
||||
}
|
||||
}
|
||||
break;
|
||||
_rstrip_continue:
|
||||
;
|
||||
}
|
||||
while (s1 <= rs1) {
|
||||
*s2 = *s1;
|
||||
s2 += 1;
|
||||
s1 += 1;
|
||||
}
|
||||
*s2 = 0;
|
||||
return stripped;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user