summaryrefslogtreecommitdiff
path: root/frontend/transpiler/MyString.h
blob: d05d84a29f54f2a8e86606665c56fcb55aae11f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#pragma once
#include "List.h"

struct StringView {
	bool error = false;
	char* value = nullptr;
	size_t length = 0;
};

struct String {
	const static int SSO_SIZE = 31;

	char defaultBuffer[String::SSO_SIZE + 1] = { '\0' };
	char* dynamicBuffer = nullptr;

	int length = 0;
	int capacity = 0;
	bool isSSO = true;

	String();
	String(const char* str);
	char* getValue();
	const char* getValueConst() const;
	void operator =(const char* str);
	void set(const char* str);
	void free();
	int toInteger();
	float toFloat();
	int indexOf(char c);
	inline bool equals(const String& other) { return strcmp(getValueConst(), other.getValueConst()) == 0; };
	inline bool equalsCstr(const char* str) { return strcmp(getValueConst(), str) == 0; };
	StringView substring(int start, int end);
};

struct StringBuffer {
	const static int BUFFER_SIZE = 31;

	int pointer = 0;
	char buffer[StringBuffer::BUFFER_SIZE + 1]; // Leave space for trailing escape character

	/* 
	* Appends the string to the buffer
	* @param str
	* @returns number of characters copied
	*/
	int add(const char* str);
	bool isFull();
	void reset();
};

struct StringBuilder {
	int bufferPointer = 0;
	int length = 0;

	StringBuffer defaultBuffer;
	List<StringBuffer> dynamicBuffer;

	StringBuffer* getCurrentBuffer();
	StringBuffer* getBufferAtIdx(int index);
	const StringBuffer* getBufferAtIdxConst(int index) const;
	void addStr(String* str);
	void addStr(const char* str);
	void addChar(char c);
	void format(const char* str, ...);
	void addInt(int value);
	void addFloat(float value);
	void replace(const char* strToReplace, const char* replaceStr);
	void removeAt(int index, int count);
	int indexOf(char c);
	int indexOf(const char* str);
	String toString();
	void clear();
	char getCharAtIdx(int index) const;
	void insert(char c, int index);
	void free();
};