This repository has been archived on 2026-05-18. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
piquant/src/string.c
Andrew Kesterson 921a9dd8bd - 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
2024-05-04 22:08:20 -04:00

140 lines
2.2 KiB
C

#include "types.h"
#include "string.h"
size_t strlen(char *ptr)
{
char *ptr2 = ptr;
if ( ptr == NULL ) {
return 0;
}
while (*ptr2 != 0x0 ) {
ptr2 += 1;
}
return ptr2 - ptr;
}
void *memset(void *s, char c, size_t n)
{
char *d = (char *)s;
if ( s == NULL ) {
return NULL;
}
while ( (d - (char *)s) < n ) {
*d = c;
d += sizeof(char);
}
return s;
}
void *memcpy(void *dest, void *src, size_t n)
{
int i = 0;
char *d = (char *)dest;
char *s = (char *)src;
if ( d == NULL || s == NULL ) {
return NULL;
}
for ( i = 0 ; i < n ; i++ ) {
*d = *s;
d += 1;
s += 1;
}
return dest;
}
int strncat(char *dest, char *src, size_t n)
{
int c = 0;
if ( dest == NULL || src == NULL ) {
return 0;
}
dest = (dest + strlen(dest));
while ( c++ < n ) {
*dest++ = *src++;
}
return c;
}
int strcmp(char *s1, char *s2)
{
int s1len = 0;
if ( s1 == NULL || s2 == NULL ) {
return 1;
}
s1len = strlen(s1);
if ( s1len != strlen(s2) ) {
return 1;
}
while ( *s1 != '\0' ) {
if ( *s1 != *s2 ) {
return 1;
}
s1 += sizeof(char);
s2 += sizeof(char);
}
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;
}