Established length-based strings
This commit is contained in:
57
src/lenstr.c
Normal file
57
src/lenstr.c
Normal file
@@ -0,0 +1,57 @@
|
||||
#include "../include/lenstr.h"
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
string CStyleToLengthString_Assign(char *cstr)
|
||||
{
|
||||
string x;
|
||||
x.chars = cstr;
|
||||
x.length = strlen(x.chars);
|
||||
return x;
|
||||
}
|
||||
|
||||
string CStyleToLengthString_Copy(char *cstr)
|
||||
{
|
||||
string x;
|
||||
x.length = strlen(cstr);
|
||||
x.chars = malloc(sizeof(char) * x.length); // Should I add +1 here for null terminator?
|
||||
char *d = x.chars;
|
||||
char *s = cstr;
|
||||
while (*d++ = *s++);
|
||||
return x;
|
||||
}
|
||||
|
||||
// Research and implement memory arenas before implementing this
|
||||
// char *LengthStringToCStyle(string str)
|
||||
// {
|
||||
// char *cstr;
|
||||
// cstr = malloc
|
||||
// }
|
||||
|
||||
string StringCopy(string str)
|
||||
{
|
||||
string x;
|
||||
x.length = str.length;
|
||||
x.chars = malloc(sizeof(char) * x.length);
|
||||
for (int i = 0; i < x.length; i++) {
|
||||
x.chars[i] = str.chars[i];
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
string StringSlice_Index(string str, size_t starti, size_t endi)
|
||||
{
|
||||
string x;
|
||||
x.chars = &str.chars[starti];
|
||||
x.length = (endi - starti);
|
||||
return x;
|
||||
}
|
||||
|
||||
string StringSlice_Length(string str, size_t starti, size_t length)
|
||||
{
|
||||
string x;
|
||||
x.chars = &str.chars[starti];
|
||||
x.length = length;
|
||||
return x;
|
||||
}
|
||||
42
src/main.c
42
src/main.c
@@ -1,7 +1,45 @@
|
||||
#include "../include/lenstr.h"
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
int main(void)
|
||||
{
|
||||
printf("hello world\n");
|
||||
// C-style to string
|
||||
string a;
|
||||
a.chars = "Manual transformation of C-style to String";
|
||||
a.length = strlen(a.chars);
|
||||
PrintStr(a);
|
||||
|
||||
// Automatic c-style to string
|
||||
string b = CStyleToLengthString_Assign("Function transformation of C-style to String");
|
||||
PrintStr(b);
|
||||
|
||||
// String assigning (shallow copy) and copying (deep copy)
|
||||
char xx[] = "String Assign Copy";
|
||||
char yy[] = "String Deep Copy";
|
||||
string x = CStyleToLengthString_Assign(xx);
|
||||
string y = CStyleToLengthString_Assign(yy);
|
||||
string c = x;
|
||||
PrintStr(c);
|
||||
string d = StringCopy(y);
|
||||
PrintStr(d);
|
||||
x.chars[3] = '!';
|
||||
y.chars[3] = '!';
|
||||
PrintStr(c);
|
||||
PrintStr(d);
|
||||
|
||||
// Manual string slicing
|
||||
string e;
|
||||
e.chars = &a.chars[7];
|
||||
e.length = 14;
|
||||
PrintStr(e);
|
||||
|
||||
// Automatic string slicing (by length or by index range)
|
||||
string f = StringSlice_Length(a, 7, 14);
|
||||
string g = StringSlice_Index(a, 7, 21);
|
||||
PrintStr(f);
|
||||
PrintStr(g);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user