New project
This commit is contained in:
92
src/text_editor/buffer.cpp
Normal file
92
src/text_editor/buffer.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
https://code.visualstudio.com/blogs/2018/03/23/text-buffer-reimplementation
|
||||
*/
|
||||
// struct Pos {
|
||||
// S32 line;
|
||||
// S32 col;
|
||||
// };
|
||||
|
||||
// struct Range {
|
||||
// Pos from; // including
|
||||
// Pos to; // one past last
|
||||
// };
|
||||
|
||||
struct Range {
|
||||
Int min;
|
||||
Int max;
|
||||
};
|
||||
|
||||
struct Buffer {
|
||||
Allocator allocator;
|
||||
U16 *data;
|
||||
Int len;
|
||||
Int cap;
|
||||
};
|
||||
|
||||
void InitBuffer(Allocator allocator, Buffer *buffer) {
|
||||
buffer->cap = 4096;
|
||||
buffer->data = AllocArray(allocator, U16, buffer->cap);
|
||||
buffer->allocator = allocator;
|
||||
}
|
||||
|
||||
void Grow(Buffer *buffer, Int change_size) {
|
||||
Int new_size = buffer->len + change_size;
|
||||
if (new_size > buffer->cap) {
|
||||
Int outside = new_size - buffer->cap;
|
||||
Int new_cap = (buffer->cap + outside) * 2;
|
||||
U16 *new_array = AllocArray(buffer->allocator, U16, new_cap);
|
||||
MemoryCopy(new_array, buffer->data, buffer->len * sizeof(U16));
|
||||
Dealloc(buffer->allocator, &buffer->data);
|
||||
buffer->cap = new_cap;
|
||||
buffer->data = new_array;
|
||||
}
|
||||
}
|
||||
|
||||
void ReplaceText(Buffer *buffer, Range range, String16 string) {
|
||||
Assert(range.max >= range.min);
|
||||
Assert(range.max >= 0 && range.max <= buffer->len);
|
||||
Assert(range.min >= 0 && range.min <= buffer->len);
|
||||
|
||||
Int size_to_remove = range.max - range.min;
|
||||
Int size_to_add = string.len;
|
||||
Int change_size = size_to_add - size_to_remove;
|
||||
Assert(change_size + buffer->len >= 0);
|
||||
Grow(buffer, change_size);
|
||||
|
||||
Int range_size = range.max - range.min;
|
||||
U16 *begin_remove = buffer->data + range.min;
|
||||
U16 *end_remove = begin_remove + range_size;
|
||||
Int remain_len = buffer->len - (range.min + range_size);
|
||||
|
||||
U16 *begin_add = begin_remove;
|
||||
U16 *end_add = begin_add + string.len;
|
||||
MemoryMove(end_add, end_remove, remain_len * sizeof(U16));
|
||||
MemoryCopy(begin_add, string.data, string.len * sizeof(U16));
|
||||
buffer->len += change_size;
|
||||
}
|
||||
|
||||
Int GetSize(Range range) {
|
||||
Assert(range.max >= range.min);
|
||||
Int result = range.max - range.min;
|
||||
return result;
|
||||
}
|
||||
|
||||
String16 GetString(Buffer &buffer, Range range = {0, INT64_MAX}) {
|
||||
range.min = Clamp(range.min, (Int)0, buffer.len);
|
||||
range.max = Clamp(range.max, (Int)0, buffer.len);
|
||||
String16 result = {(wchar_t *)buffer.data + range.min, GetSize(range)};
|
||||
return result;
|
||||
}
|
||||
|
||||
void TestBuffer() {
|
||||
Scratch scratch;
|
||||
Buffer buffer = {};
|
||||
|
||||
String16 test_string = L"Thing itself";
|
||||
InitBuffer(scratch, &buffer);
|
||||
ReplaceText(&buffer, {{}, {}}, test_string);
|
||||
Assert(buffer.cap == 4096);
|
||||
Assert(buffer.len == 12);
|
||||
String16 a = GetString(buffer);
|
||||
Assert(a == test_string);
|
||||
}
|
||||
45
src/text_editor/new_basic.cpp
Normal file
45
src/text_editor/new_basic.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
|
||||
|
||||
void Test() {
|
||||
{
|
||||
Array<int> array = {};
|
||||
for (int i = 0; i < 64; i += 1) {
|
||||
Add(&array, i);
|
||||
}
|
||||
Assert(array.len == 64);
|
||||
int i = 0;
|
||||
For(array) {
|
||||
Assert(it == i++);
|
||||
}
|
||||
Assert(Contains(array, 5));
|
||||
Assert(!Contains(array, 70));
|
||||
|
||||
int a[] = {64, 65, 66, 67};
|
||||
InsertArray(&array, a, 4, 62);
|
||||
Assert(array.len == 68);
|
||||
Assert(array[61] == 61);
|
||||
Assert(array[62] == 64);
|
||||
Assert(array[63] == 65);
|
||||
Assert(array[64] == 66);
|
||||
Assert(array[65] == 67);
|
||||
Assert(array[66] == 62);
|
||||
|
||||
Assert(Get(array, 90, 0) == 0);
|
||||
Assert(Get(array, 4, 0) == 4);
|
||||
}
|
||||
|
||||
{
|
||||
Array<int> arr = {};
|
||||
for (int i = 0; i < 32; i += 1) {
|
||||
Add(&arr, i);
|
||||
}
|
||||
|
||||
int i = 31;
|
||||
For(IterateInReverse(&arr)) {
|
||||
Assert(i == it);
|
||||
i -= 1;
|
||||
}
|
||||
Assert(i == -1);
|
||||
GetSlice(arr, 4, 7);
|
||||
}
|
||||
}
|
||||
52
src/text_editor/string16.cpp
Normal file
52
src/text_editor/string16.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
|
||||
wchar_t ToLowerCase(wchar_t a) {
|
||||
if (a >= 'A' && a <= 'Z') a += 32;
|
||||
return a;
|
||||
}
|
||||
|
||||
wchar_t ToUpperCase(wchar_t a) {
|
||||
if (a >= 'a' && a <= 'z') a -= 32;
|
||||
return a;
|
||||
}
|
||||
|
||||
bool IsWhitespace(wchar_t w) {
|
||||
bool result = w == '\n' || w == ' ' || w == '\t' || w == '\v' || w == '\r';
|
||||
return result;
|
||||
}
|
||||
|
||||
bool IsAlphabetic(wchar_t a) {
|
||||
bool result = (a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z');
|
||||
return result;
|
||||
}
|
||||
|
||||
bool IsIdent(wchar_t a) {
|
||||
bool result = (a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z') || a == '_';
|
||||
return result;
|
||||
}
|
||||
|
||||
bool IsDigit(wchar_t a) {
|
||||
bool result = a >= '0' && a <= '9';
|
||||
return result;
|
||||
}
|
||||
|
||||
bool IsAlphanumeric(wchar_t a) {
|
||||
bool result = IsDigit(a) || IsAlphabetic(a);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool AreEqual(String16 a, String16 b, unsigned ignore_case = false) {
|
||||
if (a.len != b.len) return false;
|
||||
for (int64_t i = 0; i < a.len; i++) {
|
||||
wchar_t A = a.data[i];
|
||||
wchar_t B = b.data[i];
|
||||
if (ignore_case) {
|
||||
A = ToLowerCase(A);
|
||||
B = ToLowerCase(B);
|
||||
}
|
||||
if (A != B)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
inline bool operator==(String16 a, String16 b) { return AreEqual(a, b); }
|
||||
inline bool operator!=(String16 a, String16 b) { return !AreEqual(a, b); }
|
||||
25
src/text_editor/text_editor.cpp
Normal file
25
src/text_editor/text_editor.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#define BASIC_IMPL
|
||||
#include "../basic/basic.h"
|
||||
#include "new_basic.cpp"
|
||||
#include "string16.cpp"
|
||||
|
||||
#include "buffer.cpp"
|
||||
#include "raylib.h"
|
||||
|
||||
int main(void) {
|
||||
InitScratch();
|
||||
Test();
|
||||
TestBuffer();
|
||||
return 0;
|
||||
|
||||
InitWindow(1280, 720, "hello :)");
|
||||
SetTargetFPS(60);
|
||||
while (!WindowShouldClose()) {
|
||||
BeginDrawing();
|
||||
ClearBackground(RAYWHITE);
|
||||
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
|
||||
EndDrawing();
|
||||
}
|
||||
CloseWindow();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user