Established length-based strings

This commit is contained in:
kilroy
2025-11-28 19:31:49 -05:00
parent 62c202d4e7
commit b7ce065002
3 changed files with 119 additions and 2 deletions

22
include/lenstr.h Normal file
View File

@@ -0,0 +1,22 @@
#ifndef LENSTR_H
#define LENSTR_H
/* Length-based strings
* and methods to go with them. */
#define PrintStr(str) printf("%.*s\n", str.length, str.chars)
#define StrLit(s) (string) {s, sizeof(s)-1}
typedef struct {
char *chars;
size_t length;
} string;
string CStyleToLengthString_Assign(char *cstr); // StrLit(s) macro but in function form.
string CStyleToLengthString_Copy(char *cstr);
string StringCopy(string str);
string StringSlice_Index(string str, size_t starti, size_t endi);
string StringSlice_Length(string str, size_t starti, size_t length);
#endif // LENSTR_H

57
src/lenstr.c Normal file
View 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;
}

View File

@@ -1,7 +1,45 @@
#include "../include/lenstr.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.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; return 0;
} }