Improving string for C++

This commit is contained in:
Krzosa Karol
2024-01-13 17:09:38 +01:00
parent 3408f3a1ff
commit 1dc0eceeba
12 changed files with 89 additions and 55 deletions

View File

@@ -6,9 +6,8 @@ int ReturnValue = 0;
int Main() {
Strs cc = CMDLine.get("cc"_s, ON_WINDOWS("cl"_s) ON_MAC("clang"_s) ON_LINUX("gcc"_s));
Strs files = ListDir("../tests");
CompileFiles(cc, {"../tests/main_core_as_header.cpp", "../core_library/core.c"});
For(files) {
if (S8_Find(it, "test_"_s)) {
if (S8_Seek(it, "test_"_s)) {
CompileFiles(cc, it);
}
}

View File

@@ -224,7 +224,7 @@ int main(int argc, char **argv) {
S8_String it = S8_MakeFromChar(argv[i]);
int64_t idx = 0;
if (S8_Find(it, "="_s, 0, &idx)) {
if (S8_Seek(it, "="_s, 0, &idx)) {
S8_String key = S8_GetPrefix(it, idx);
S8_String value = S8_Skip(it, idx + 1);
if (key.len == 0) ReportError(it);

View File

@@ -24,10 +24,10 @@ int main(int argument_count, char **arguments) {
SRC_InitCache(Perm, cache_filename);
// Search for build file in the project directory
S8_String build_file = {0};
S8_String build_file = {};
{
for (OS_FileIter it = OS_IterateFiles(Perm, S8_Lit("..")); OS_IsValid(it); OS_Advance(&it)) {
if (S8_Find(it.filename, S8_Lit("build_file.c"), S8_IgnoreCase)) {
if (S8_Seek(it.filename, S8_Lit("build_file.c"), S8_IgnoreCase)) {
build_file = it.absolute_path;
}
}

View File

@@ -3,8 +3,8 @@
#include "../standalone_libraries/stb_sprintf.h"
#include "../standalone_libraries/io.h"
#include "../standalone_libraries/arena.h"
#include "../standalone_libraries/string.h"
#include "../standalone_libraries/unicode.h"
#include "../standalone_libraries/string.h"
#include "../standalone_libraries/hash.h"
#include "../standalone_libraries/linked_list.h"
#include "../standalone_libraries/regex.h"

View File

@@ -678,7 +678,7 @@ OS_API bool OS_ExpandIncludesList(MA_Arena *arena, S8_List *out, S8_String filep
S8_String include = S8_Lit("#include \"");
for (;;) {
int64_t idx = -1;
if (S8_Find(c, include, 0, &idx)) {
if (S8_Seek(c, include, 0, &idx)) {
S8_String str_to_add = S8_GetPrefix(c, idx);
S8_AddNode(arena, out, str_to_add);
S8_String save = c;
@@ -706,8 +706,8 @@ OS_API bool OS_ExpandIncludesList(MA_Arena *arena, S8_List *out, S8_String filep
}
OS_API S8_String OS_ExpandIncludes(MA_Arena *arena, S8_String filepath) {
S8_List out = {0};
S8_String result = {0};
S8_List out = S8_MakeEmptyList();
S8_String result = S8_MakeEmpty();
MA_ScratchScope(s) {
OS_ExpandIncludesList(s.arena, &out, filepath);
result = S8_Merge(arena, out);

View File

@@ -1,11 +1,7 @@
// Quick and dirty filesystem operations
#ifndef OS_API
#ifdef __cplusplus
#define OS_API extern "C"
#else
#define OS_API
#endif
#define OS_API
#endif
typedef enum OS_Result {

View File

@@ -258,7 +258,7 @@ S8_API S8_String S8_ToUpperCase(S8_Allocator allocator, S8_String s) {
return copy;
}
S8_API bool S8_Find(S8_String string, S8_String find, S8_FindFlag flags, int64_t *index_out) {
S8_API bool S8_Seek(S8_String string, S8_String find, S8_FindFlag flags, int64_t *index_out) {
bool ignore_case = flags & S8_FindFlag_IgnoreCase ? true : false;
bool result = false;
if (flags & S8_FindFlag_MatchFindLast) {
@@ -288,12 +288,18 @@ S8_API bool S8_Find(S8_String string, S8_String find, S8_FindFlag flags, int64_t
return result;
}
S8_API int64_t S8_Find(S8_String string, S8_String find, S8_FindFlag flag) {
int64_t result = -1;
S8_Seek(string, find, flag, &result);
return result;
}
S8_API S8_List S8_Split(S8_Allocator allocator, S8_String string, S8_String find, S8_SplitFlag flags) {
S8_List result = S8_MakeEmptyList();
int64_t index = 0;
S8_FindFlag find_flag = flags & S8_SplitFlag_IgnoreCase ? S8_FindFlag_IgnoreCase : S8_FindFlag_None;
while (S8_Find(string, find, find_flag, &index)) {
while (S8_Seek(string, find, find_flag, &index)) {
S8_String before_match = S8_Make(string.str, index);
S8_AddNode(allocator, &result, before_match);
if (flags & S8_SplitFlag_SplitInclusive) {
@@ -350,7 +356,7 @@ S8_API S8_List S8_FindAll(S8_Allocator allocator, S8_String string, S8_String fi
int64_t index = 0;
S8_FindFlag find_flag = ignore_case ? S8_FindFlag_IgnoreCase : 0;
while (S8_Find(string, find, find_flag, &index)) {
while (S8_Seek(string, find, find_flag, &index)) {
S8_String match = S8_Make(string.str + index, find.len);
S8_AddNode(allocator, &result, match);
string = S8_Skip(string, index + find.len);
@@ -360,20 +366,20 @@ S8_API S8_List S8_FindAll(S8_Allocator allocator, S8_String string, S8_String fi
S8_API S8_String S8_ChopLastSlash(S8_String s) {
S8_String result = s;
S8_Find(s, S8_Lit("/"), S8_FindFlag_MatchFindLast, &result.len);
S8_Seek(s, S8_Lit("/"), S8_FindFlag_MatchFindLast, &result.len);
return result;
}
S8_API S8_String S8_ChopLastPeriod(S8_String s) {
S8_String result = s;
S8_Find(s, S8_Lit("."), S8_FindFlag_MatchFindLast, &result.len);
S8_Seek(s, S8_Lit("."), S8_FindFlag_MatchFindLast, &result.len);
return result;
}
S8_API S8_String S8_SkipToLastSlash(S8_String s) {
int64_t pos;
S8_String result = s;
if (S8_Find(s, S8_Lit("/"), S8_FindFlag_MatchFindLast, &pos)) {
if (S8_Seek(s, S8_Lit("/"), S8_FindFlag_MatchFindLast, &pos)) {
result = S8_Skip(result, pos + 1);
}
return result;
@@ -382,7 +388,7 @@ S8_API S8_String S8_SkipToLastSlash(S8_String s) {
S8_API S8_String S8_SkipToLastPeriod(S8_String s) {
int64_t pos;
S8_String result = s;
if (S8_Find(s, S8_Lit("."), S8_FindFlag_MatchFindLast, &pos)) {
if (S8_Seek(s, S8_Lit("."), S8_FindFlag_MatchFindLast, &pos)) {
result = S8_Skip(result, pos + 1);
}
return result;

View File

@@ -4,11 +4,7 @@
#include <stdbool.h>
#ifndef S8_API
#ifdef __cplusplus
#define S8_API extern "C"
#else
#define S8_API
#endif
#define S8_API
#endif
#ifdef __cplusplus
@@ -30,6 +26,35 @@ S8_API int64_t S8_Length(char *string);
struct S8_String {
char *str;
int64_t len;
#if defined(__cplusplus)
S8_String() = default;
S8_String(char *s) : str(s), len(S8_Length(s)) {}
S8_String(char *s, int64_t l) : str(s), len(l) {}
S8_String(const char *s) : str((char *)s), len(S8_Length((char *)s)) {}
S8_String(const char *s, int64_t l) : str((char *)s), len(l) {}
#if defined(UTF_HEADER)
struct Iter {
UTF8_Iter i;
Iter operator++(int) {
Iter ret = *this;
UTF8_Advance(&i);
return ret;
}
Iter &operator++() {
UTF8_Advance(&i);
return *this;
}
friend bool operator!=(const Iter &a, const Iter &b) { return a.i.item != b.i.item; }
UTF8_Iter &operator*() { return i; }
};
Iter begin() { return {UTF8_IterateEx(str, (int)len)}; }
Iter end() { return {}; }
#endif // UTF_HEADER
#endif // __cplusplus
};
struct S8_Node {
@@ -44,8 +69,6 @@ struct S8_List {
S8_Node *last;
};
const int S8_IgnoreCase = true;
typedef int S8_FindFlag;
enum {
S8_FindFlag_None = 0,
@@ -60,6 +83,8 @@ enum {
S8_SplitFlag_SplitInclusive = 2,
};
const bool S8_IgnoreCase = true;
#if defined(__has_attribute)
#if __has_attribute(format)
#define S8__PrintfFormat(fmt, va) __attribute__((format(printf, fmt, va)))
@@ -83,12 +108,6 @@ enum {
#define S8_For(it, x) for (S8_Node *it = (x).first; it; it = it->next)
#if defined(__cplusplus)
S8_API bool S8_AreEqual(S8_String a, S8_String b, unsigned ignore_case);
inline bool operator==(S8_String a, S8_String b) { return S8_AreEqual(a, b, false); }
inline S8_String operator""_s(const char *str, size_t size) { return {(char *)str, (int64_t)size}; }
#endif
S8_API char CHAR_ToLowerCase(char a);
S8_API char CHAR_ToUpperCase(char a);
S8_API bool CHAR_IsWhitespace(char w);
@@ -111,7 +130,8 @@ S8_API S8_String S8_Trim(S8_String string);
S8_API S8_String S8_TrimEnd(S8_String string);
S8_API S8_String S8_ToLowerCase(S8_Allocator allocator, S8_String s);
S8_API S8_String S8_ToUpperCase(S8_Allocator allocator, S8_String s);
S8_API bool S8_Find(S8_String string, S8_String find, S8_FindFlag flags S8_IF_CPP(= S8_FindFlag_None), int64_t *index_out S8_IF_CPP(= 0));
S8_API bool S8_Seek(S8_String string, S8_String find, S8_FindFlag flags S8_IF_CPP(= S8_FindFlag_None), int64_t *index_out S8_IF_CPP(= 0));
S8_API int64_t S8_Find(S8_String string, S8_String find, S8_FindFlag flags S8_IF_CPP(= S8_FindFlag_None));
S8_API S8_String S8_ChopLastSlash(S8_String s);
S8_API S8_String S8_ChopLastPeriod(S8_String s);
S8_API S8_String S8_SkipToLastSlash(S8_String s);
@@ -128,7 +148,7 @@ S8_API S8_String S8_FormatV(S8_Allocator allocator, const char *str, va_list arg
S8_API S8_String S8_Format(S8_Allocator allocator, const char *str, ...) S8__PrintfFormat(2, 3);
S8_API S8_List S8_Split(S8_Allocator allocator, S8_String string, S8_String find, S8_SplitFlag flags S8_IF_CPP(= S8_SplitFlag_None));
S8_API S8_String S8_MergeWithSeparator(S8_Allocator allocator, S8_List list, S8_String separator S8_IF_CPP(= " "_s));
S8_API S8_String S8_MergeWithSeparator(S8_Allocator allocator, S8_List list, S8_String separator S8_IF_CPP(= S8_Lit(" ")));
S8_API S8_String S8_Merge(S8_Allocator allocator, S8_List list);
S8_API S8_String S8_ReplaceAll(S8_Allocator allocator, S8_String string, S8_String replace, S8_String with, bool ignore_case S8_IF_CPP(= false));
S8_API S8_List S8_FindAll(S8_Allocator allocator, S8_String string, S8_String find, bool ignore_case S8_IF_CPP(= false));
@@ -144,3 +164,9 @@ S8_API S8_List S8_ConcatLists(S8_Allocator allocator, S8_List a, S8_List b);
S8_API S8_Node *S8_AddNode(S8_Allocator allocator, S8_List *list, S8_String string);
S8_API S8_Node *S8_Add(S8_Allocator allocator, S8_List *list, S8_String string);
S8_API S8_String S8_AddF(S8_Allocator allocator, S8_List *list, const char *str, ...) S8__PrintfFormat(3, 4);
#if defined(__cplusplus)
inline S8_String operator""_s(const char *str, size_t size) { return {(char *)str, (int64_t)size}; }
inline bool operator==(S8_String a, S8_String b) { return S8_AreEqual(a, b, 0); }
inline bool operator!=(S8_String a, S8_String b) { return !S8_AreEqual(a, b, 0); }
#endif

View File

@@ -1,4 +1,5 @@
#pragma once
#define UTF_HEADER
#include <stdint.h>
typedef struct UTF32_Result UTF32_Result;
typedef struct UTF8_Result UTF8_Result;

View File

@@ -1,10 +1,14 @@
#include "../core_library/core.c"
int main() {
MA_Scratch scratch;
for (OS_FileIter it = OS_IterateFiles(scratch, "../"_s); OS_IsValid(it); OS_Advance(&it)) {
IO_Printf("is_directory: %d\n", it.is_directory);
IO_Printf("absolute_path: %.*s\n", S8_Expand(it.absolute_path));
IO_Printf("relative_path: %.*s\n", S8_Expand(it.relative_path));
S8_String s = "mrówka";
bool found_two_byte = false;
For(s) {
if (it.utf8_codepoint_byte_size == 2) {
found_two_byte = true;
IO_Assert(it.i == 2);
}
}
IO_Assert(found_two_byte);
}

View File

@@ -1,8 +0,0 @@
#include "../core_library/core.h"
int main() {
MA_Arena arena = {};
int *a = MA_PushStruct(&arena, int);
*a = 10;
return 0;
}

View File

@@ -67,11 +67,21 @@ int main() {
IO_Assert(OS_IsAbsolute(work_path));
IO_Assert(OS_IsAbsolute(abs_path));
IO_Assert(S8_Find(exe_path, S8_Lit("/test_filesystem"), 0, 0));
IO_Assert(S8_Find(exe_path, S8_Lit("/build"), 0, 0));
IO_Assert(S8_Find(dir_path, S8_Lit("/build"), 0, 0));
IO_Assert(S8_Find(work_path, S8_Lit("/build"), 0, 0));
IO_Assert(S8_Find(abs_path, S8_Lit("/tests/data"), 0, 0));
IO_Assert(!S8_Find(abs_path, S8_Lit("../"), 0, 0));
IO_Assert(S8_Seek(exe_path, S8_Lit("/test_filesystem"), 0, 0));
IO_Assert(S8_Seek(exe_path, S8_Lit("/build"), 0, 0));
IO_Assert(S8_Seek(dir_path, S8_Lit("/build"), 0, 0));
IO_Assert(S8_Seek(work_path, S8_Lit("/build"), 0, 0));
IO_Assert(S8_Seek(abs_path, S8_Lit("/tests/data"), 0, 0));
IO_Assert(!S8_Seek(abs_path, S8_Lit("../"), 0, 0));
}
{
for (OS_FileIter it = OS_IterateFiles(&arena, S8_Lit("..")); OS_IsValid(it); OS_Advance(&it)) {
if (it.is_directory) {
IO_Assert(it.absolute_path.str[it.absolute_path.len - 1] == '/');
}
IO_Assert(!S8_Seek(it.absolute_path, S8_Lit(".."), 0, 0));
IO_Assert(S8_Seek(it.relative_path, S8_Lit(".."), 0, 0));
}
}
}