Simplify folder sturcture + String refactor

1) Over time the total amount of files has decreased, and so it made sense to just simplify the folder structure and remove all of the subfolders.
2) Refactor the String class to utilize the existing Vector and StackVector classes instead of managing everything itself.
This commit is contained in:
Noel Berry 2022-10-01 13:29:51 -07:00
parent c94e372e7d
commit dcd3e11b16
47 changed files with 1018 additions and 1091 deletions

View File

@ -6,25 +6,25 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_library(blah
src/app.cpp
src/filesystem.cpp
src/common.cpp
src/time.cpp
src/input.cpp
src/stream.cpp
src/graphics.cpp
src/containers/str.cpp
src/drawing/batch.cpp
src/drawing/spritefont.cpp
src/drawing/subtexture.cpp
src/images/aseprite.cpp
src/images/font.cpp
src/images/image.cpp
src/images/packer.cpp
src/internal/renderer_opengl.cpp
src/internal/renderer_d3d11.cpp
src/internal/platform_sdl2.cpp
src/internal/platform_win32.cpp
src/blah_app.cpp
src/blah_filesystem.cpp
src/blah_common.cpp
src/blah_time.cpp
src/blah_input.cpp
src/blah_stream.cpp
src/blah_graphics.cpp
src/blah_string.cpp
src/blah_batch.cpp
src/blah_spritefont.cpp
src/blah_subtexture.cpp
src/blah_aseprite.cpp
src/blah_font.cpp
src/blah_image.cpp
src/blah_packer.cpp
src/internal/blah_renderer_opengl.cpp
src/internal/blah_renderer_d3d11.cpp
src/internal/blah_platform_sdl2.cpp
src/internal/blah_platform_win32.cpp
)
target_include_directories(blah

View File

@ -1,27 +1,22 @@
#pragma once
#include "blah/common.h"
#include "blah/app.h"
#include "blah/filesystem.h"
#include "blah/time.h"
#include "blah/input.h"
#include "blah/stream.h"
#include "blah/graphics.h"
#include "blah/containers/vector.h"
#include "blah/containers/stackvector.h"
#include "blah/containers/str.h"
#include "blah/drawing/batch.h"
#include "blah/drawing/spritefont.h"
#include "blah/drawing/subtexture.h"
#include "blah/images/aseprite.h"
#include "blah/images/font.h"
#include "blah/images/image.h"
#include "blah/images/packer.h"
#include "blah/math/calc.h"
#include "blah/math/spatial.h"
#include "blah/math/color.h"
#include "blah/math/ease.h"
#include "blah_app.h"
#include "blah_aseprite.h"
#include "blah_batch.h"
#include "blah_calc.h"
#include "blah_color.h"
#include "blah_common.h"
#include "blah_ease.h"
#include "blah_filesystem.h"
#include "blah_font.h"
#include "blah_graphics.h"
#include "blah_image.h"
#include "blah_input.h"
#include "blah_packer.h"
#include "blah_spatial.h"
#include "blah_spritefont.h"
#include "blah_stackvector.h"
#include "blah_string.h"
#include "blah_stream.h"
#include "blah_subtexture.h"
#include "blah_time.h"
#include "blah_vector.h"

View File

@ -1,321 +0,0 @@
#pragma once
#include <blah/common.h>
#include <blah/containers/vector.h>
#include <stdarg.h>
#include <stdio.h>
namespace Blah
{
template<int T> class StrOf;
using String = StrOf<64>;
using FilePath = StrOf<260>;
// A simple String implementation
class Str
{
public:
Str() { m_buffer = empty_buffer; m_length = m_capacity = m_local_size = 0; }
Str(const char* start, const char* end = nullptr) : Str() { set(start, end); }
Str(const Str& str) : Str() { set(str); }
char& operator[](int index) { return data()[index]; }
const char& operator[](int index) const { return data()[index]; }
// equality operators
bool operator==(const Str& rhs) const;
bool operator!=(const Str& rhs) const;
bool operator==(const char* rhs) const;
bool operator!=(const char* rhs) const;
// assignment operator
Str& operator=(const Str& rhs) { set(rhs.cstr(), rhs.cstr() + rhs.m_length); return *this; }
Str& operator=(const char* rhs) { set(rhs, nullptr); return *this; }
// append string
Str& operator+=(const Str& rhs) { return append(rhs); }
// append cstr
Str& operator+=(const char* rhs) { return append(rhs); }
// append char
Str& operator+=(const char& rhs) { return append(rhs); }
// combine string
Str operator+(const Str& rhs) { Str str; str.append(*this).append(rhs); return str; }
// combine cstr
Str operator+(const char* rhs) { Str str; str.append(*this).append(rhs); return str; }
// combine char
Str operator+(const char& rhs) { Str str; str.append(*this).append(rhs); return str; }
// implicit cast to cstr
operator char* () { return cstr(); }
// implicit cast to cstr
operator const char* () const { return cstr(); }
// returns a pointer to the null-terminated string buffer
char* cstr() { return data(); }
// returns a pointer to the null-terminated string buffer
const char* cstr() const { return data(); }
// returns a pointer to the start of the buffer
const char* begin() const { return data(); }
// returns a pointer to the end of the buffer
const char* end() const { return data() + m_length; }
// returns the length of the string
int length() const { return m_length; }
// returns the capacity of the string
int capacity() const { return m_capacity; }
// returns the capacity of the string's stack buffer
int stack_capacity() const { return m_local_size; }
// sets the length of the string.
// this does not set the value of the string!
void set_length(int length);
// ensures the string has the given capacity
void reserve(int capacity);
// Returns the unicode value at the given index.
// Assumes the index is a valid utf8 starting point.
u32 utf8_at(int index) const;
// Returns the byte-length of the utf8 character.
// Assumes the index is a valid utf8 starting point.
int utf8_length(int index) const;
// appends the given character
Str& append(char c);
// appends the given unicode character
Str& append(u32 c);
// appends the given c string
Str& append(const char* start, const char* end = nullptr);
// appends the given string
Str& append(const Str& str, int start = 0, int end = -1);
// appends the given formatted string
Str& append_fmt(const char* fmt, ...);
// appends a utf16 string
Str& append_utf16(const u16* start, const u16* end = nullptr, bool swapEndian = false);
// trims whitespace
Str& trim();
// returns true if the string begins with the given string
bool starts_with(const Str& str, bool ignore_case = false) const { return starts_with(str.cstr(), ignore_case); }
// returns true if the string begins with the given string
bool equals(const Str& str, bool ignore_case = false) const { return str.length() == length() && starts_with(str.cstr(), ignore_case); }
// returns true if the string begins with the given string
bool starts_with(const char* str, bool ignore_case = false) const;
// returns true if the string contains with the given string
bool contains(const Str& str, bool ignore_case = false) const { return contains(str.cstr(), ignore_case); }
// returns true if the string contains with the given string
bool contains(const char* str, bool ignore_case = false) const;
// returns true if the string ends with the given string
bool ends_with(const Str& str, bool ignore_case = false) const { return ends_with(str.cstr(), ignore_case); }
// returns true if the string ends with the given string
bool ends_with(const char* str, bool ignore_case = false) const;
// returns the first index of the given character, or -1 if it isn't found
int first_index_of(char ch) const;
// returns the last index of the given character, or -1 if it isn't found
int last_index_of(char ch) const;
// returns a substring of the string
String substr(int start) const;
// returns a substring of the string
String substr(int start, int end) const;
// Splits the string into a vector of strings
Vector<String> split(char ch) const;
// replaces all occurances of old string with the new string
Str& replace(const Str& old_str, const Str& new_str);
// replaces all occurances of the given character in the string
Str& replace(char c, char r);
// checks if the string has a length of 0
bool empty() const { return m_length <= 0; }
// clears the string length to 0
void clear();
// clears and disposes the internal string buffer
void dispose();
virtual ~Str()
{
if (m_buffer != nullptr && m_buffer != empty_buffer)
delete[] m_buffer;
}
protected:
Str(int local_size)
{
m_buffer = nullptr;
m_length = 0;
m_capacity = local_size;
m_local_size = local_size;
}
// returns a pointer to the heap buffer or to our stack allocation
virtual char* data() { return m_buffer; }
// returns a pointer to the heap buffer or to our stack allocation
virtual const char* data() const { return m_buffer; }
// assigns the contents of the string
void set(const Str& str) { set(str.cstr(), str.cstr() + str.m_length); }
// assigns the contents of the string
void set(const char* start, const char* end = nullptr);
char* m_buffer;
private:
static char empty_buffer[1];
int m_length;
int m_capacity;
int m_local_size;
};
// combine string
inline Str operator+(const Str& lhs, const Str& rhs) { Str str; str.append(lhs).append(rhs); return str; }
// A string with a local stack buffer of size T
template<int T>
class StrOf : public Str
{
private:
char m_local_buffer[T];
public:
StrOf() : Str(T) { m_local_buffer[0] = '\0'; }
StrOf(const char* rhs, const char* end = nullptr) : Str(T) { m_local_buffer[0] = '\0'; set(rhs, end); }
StrOf(const Str& rhs) : Str(T) { m_local_buffer[0] = '\0'; set(rhs); }
StrOf(const StrOf& rhs) : Str(T) { m_local_buffer[0] = '\0'; set(rhs); }
// assignment operators
StrOf& operator=(const char* rhs) { set(rhs); return *this; }
StrOf& operator=(const Str& rhs) { set(rhs); return *this; }
StrOf& operator=(const StrOf& rhs) { set(rhs); return *this; }
// either return stack or heap buffer depending on which is in-use
char* data() override { return m_buffer != nullptr ? m_buffer : m_local_buffer; }
const char* data() const override { return m_buffer != nullptr ? m_buffer : m_local_buffer; }
// creates a string from the format
static StrOf fmt(const char* str, ...);
};
template<int T>
StrOf<T> StrOf<T>::fmt(const char* fmt, ...)
{
StrOf<T> str;
int add, diff;
// determine arg length
va_list args;
va_start(args, fmt);
add = vsnprintf(NULL, 0, fmt, args);
va_end(args);
// reserve
auto len = str.length();
str.set_length(len + add);
diff = str.capacity() - len;
if (diff <= 0) diff = 0;
// print out
va_start(args, fmt);
vsnprintf(str.cstr() + len, (size_t)diff, fmt, args);
va_end(args);
return str;
}
struct CaseInsenstiveStringHash
{
std::size_t operator()(const Blah::Str& key) const
{
std::size_t result = 2166136261U;
for (auto& it : key)
{
if (it >= 'A' && it <= 'Z')
result ^= (static_cast<std::size_t>(it) - 'A' + 'a');
else
result ^= static_cast<std::size_t>(it);
result *= 16777619U;
}
return result;
}
};
struct CaseInsenstiveStringCompare
{
bool operator() (const Str& lhs, const Str& rhs) const
{
return lhs.length() == rhs.length() && lhs.starts_with(rhs, true);
}
};
}
namespace std
{
template <>
struct hash<Blah::Str>
{
std::size_t operator()(const Blah::Str& key) const
{
std::size_t result = 2166136261U;
for (auto& it : key)
{
result ^= static_cast<std::size_t>(it);
result *= 16777619U;
}
return result;
}
};
template <int T>
struct hash<Blah::StrOf<T>>
{
std::size_t operator()(const Blah::StrOf<T>& key) const
{
std::size_t result = 2166136261U;
for (auto& it : key)
{
result ^= static_cast<std::size_t>(it);
result *= 16777619U;
}
return result;
}
};
}

View File

@ -1,7 +1,7 @@
#pragma once
#include <blah/common.h>
#include <blah/graphics.h>
#include <blah/math/spatial.h>
#include <blah_common.h>
#include <blah_graphics.h>
#include <blah_spatial.h>
namespace Blah
{

View File

@ -1,9 +1,9 @@
#pragma once
#include <blah/common.h>
#include <blah/math/color.h>
#include <blah/images/image.h>
#include <blah/containers/str.h>
#include <blah/stream.h>
#include <blah_common.h>
#include <blah_color.h>
#include <blah_image.h>
#include <blah_string.h>
#include <blah_stream.h>
namespace Blah
{

View File

@ -1,11 +1,11 @@
#pragma once
#include <blah/containers/vector.h>
#include <blah/containers/str.h>
#include <blah/drawing/spritefont.h>
#include <blah/drawing/subtexture.h>
#include <blah/math/spatial.h>
#include <blah/math/color.h>
#include <blah/graphics.h>
#include <blah_vector.h>
#include <blah_string.h>
#include <blah_spritefont.h>
#include <blah_subtexture.h>
#include <blah_spatial.h>
#include <blah_color.h>
#include <blah_graphics.h>
namespace Blah
{

View File

@ -1,6 +1,5 @@
#pragma once
#include <blah/common.h>
#include <math.h>
#include <blah_common.h>
namespace Blah
{

View File

@ -1,7 +1,7 @@
#pragma once
#include <blah/common.h>
#include <blah/containers/str.h>
#include <blah/math/spatial.h>
#include <blah_common.h>
#include <blah_string.h>
#include <blah_spatial.h>
#define BLAH_HEX_VALUE(n) ((n >= '0' && n <= '9') ? (n - '0') : ((n >= 'A' && n <= 'F') ? (10 + n - 'A') : ((n >= 'a' && n <= 'f') ? (10 + n - 'a') : 0)))

View File

@ -1,10 +1,29 @@
#pragma once
#include <new> // for in-place constructors, for Vector/StackVector
#include <utility> // for std::move, std::forward
#include <stdint.h> // for integer types
#include <stddef.h> // for std::size_t
#include <stdlib.h> // for abort
#include <stdarg.h> // for string format methods
#include <math.h> // for standard lib math functions used in blah_calc.h
#include <new> // for in-place constructors, for Vector/StackVector
#include <utility> // for std::move
// Numeric Types
namespace Blah
{
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;
using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using f32 = float;
using f64 = double;
using size_t = std::size_t;
}
// Aborts
#include <stdlib.h> // for abort
#ifdef _WIN32
#define BLAH_ABORT() do { __debugbreak(); ::exit(1); } while(0)
#else
@ -34,24 +53,6 @@
} \
} while(0)
// Numeric Types
#include <stdint.h> // for integer types
#include <stddef.h> // for size_t type
namespace Blah
{
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;
using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using f32 = float;
using f64 = double;
using size_t = std::size_t;
}
// Logging
namespace Blah
{

View File

@ -1,6 +1,6 @@
#pragma once
#include <blah/common.h>
#include <blah/math/calc.h>
#include <blah_common.h>
#include <blah_calc.h>
namespace Blah
{

View File

@ -1,7 +1,7 @@
#pragma once
#include <blah/common.h>
#include <blah/containers/str.h>
#include <blah/containers/vector.h>
#include <blah_common.h>
#include <blah_string.h>
#include <blah_vector.h>
namespace Blah
{

View File

@ -1,10 +1,10 @@
#pragma once
#include <blah/common.h>
#include <blah/stream.h>
#include <blah/images/image.h>
#include <blah/containers/str.h>
#include <blah/containers/vector.h>
#include <blah/filesystem.h>
#include <blah_common.h>
#include <blah_stream.h>
#include <blah_image.h>
#include <blah_string.h>
#include <blah_vector.h>
#include <blah_filesystem.h>
namespace Blah
{

View File

@ -1,14 +1,15 @@
#pragma once
#include <blah/common.h>
#include <blah/containers/vector.h>
#include <blah/containers/stackvector.h>
#include <blah/containers/str.h>
#include <blah/math/spatial.h>
#include <blah/images/image.h>
#include <blah/stream.h>
#include <blah_common.h>
#include <blah_vector.h>
#include <blah_stackvector.h>
#include <blah_string.h>
#include <blah_spatial.h>
#include <blah_image.h>
namespace Blah
{
class Stream;
class Shader; using ShaderRef = Ref<Shader>;
class Texture; using TextureRef = Ref<Texture>;
class Target; using TargetRef = Ref<Target>;

View File

@ -1,8 +1,8 @@
#pragma once
#include <blah/math/color.h>
#include <blah/math/spatial.h>
#include <blah/filesystem.h>
#include <blah/stream.h>
#include <blah_color.h>
#include <blah_spatial.h>
#include <blah_filesystem.h>
#include <blah_stream.h>
namespace Blah
{

View File

@ -1,8 +1,8 @@
#pragma once
#include <blah/common.h>
#include <blah/math/spatial.h>
#include <blah/containers/str.h>
#include <blah/containers/stackvector.h>
#include <blah_common.h>
#include <blah_spatial.h>
#include <blah_string.h>
#include <blah_stackvector.h>
// These are generally copied from the SDL2 Scancode Keys,
// which are in turn based on the USB standards:

View File

@ -1,12 +1,12 @@
#pragma once
#include <blah/common.h>
#include <blah/images/image.h>
#include <blah/math/color.h>
#include <blah/math/spatial.h>
#include <blah/containers/str.h>
#include <blah/containers/vector.h>
#include <blah/stream.h>
#include <blah/filesystem.h>
#include <blah_common.h>
#include <blah_image.h>
#include <blah_color.h>
#include <blah_spatial.h>
#include <blah_string.h>
#include <blah_vector.h>
#include <blah_stream.h>
#include <blah_filesystem.h>
namespace Blah
{

View File

@ -1,6 +1,6 @@
#pragma once
#include <blah/common.h>
#include <blah/math/calc.h>
#include <blah_common.h>
#include <blah_calc.h>
namespace Blah
{

View File

@ -1,9 +1,9 @@
#pragma once
#include <blah/common.h>
#include <blah/containers/str.h>
#include <blah/containers/vector.h>
#include <blah/drawing/subtexture.h>
#include <blah/images/font.h>
#include <blah_common.h>
#include <blah_string.h>
#include <blah_vector.h>
#include <blah_subtexture.h>
#include <blah_font.h>
namespace Blah
{

View File

@ -1,5 +1,5 @@
#pragma once
#include <blah/common.h>
#include <blah_common.h>
namespace Blah
{
@ -10,7 +10,7 @@ namespace Blah
class StackVector
{
private:
char m_buffer[sizeof(T) * Capacity];
u8 m_buffer[sizeof(T) * Capacity];
int m_count;
public:

View File

@ -1,9 +1,9 @@
#pragma once
#include <blah/common.h>
#include <blah/containers/str.h>
#include <blah/containers/vector.h>
#include <blah/math/calc.h>
#include <blah/filesystem.h>
#include <blah_common.h>
#include <blah_string.h>
#include <blah_vector.h>
#include <blah_calc.h>
#include <blah_filesystem.h>
namespace Blah
{

371
include/blah_string.h Normal file
View File

@ -0,0 +1,371 @@
#pragma once
#include <blah_common.h>
#include <blah_vector.h>
#include <blah_stackvector.h>
namespace Blah
{
class BaseString
{
public:
const char* cstr() const { return s_ptr(); }
char* cstr() { return s_ptr(); }
const char* begin() const { return s_ptr(); }
char* begin() { return s_ptr(); }
const char* end() const { return s_ptr() + length(); }
char* end() { return s_ptr() + length(); }
char& operator[](int index)
{
BLAH_ASSERT(index >= 0 && index < length(), "Index out of range");
return s_ptr()[index];
}
const char& operator[](int index) const
{
BLAH_ASSERT(index >= 0 && index < length(), "Index out of range");
return s_ptr()[index];
}
operator char* () { return cstr(); }
operator const char* () const { return cstr(); }
void assign(const char* cstr, const char* cstr_end = nullptr);
void append(const char* cstr, const char* cstr_end = nullptr);
void append(const u16* u16_cstr, const u16* u16_cstr_end = nullptr, bool swap_endian = false);
void append(char ch, int count = 1);
void append(u32 unicode);
void append_fmt(const char* fmt, ...);
bool starts_with(const char* cstr, bool ignore_case = false) const;
bool ends_with(const char* cstr, bool ignore_case = false) const;
bool contains(const char* cstr, bool ignore_case = false) const;
int first_index_of(char ch) const;
int last_index_of(char ch) const;
int length() const
{
int n = s_len() - 1; // reduce by 1 for null-terminator
return (n > 0 ? n : 0); // safety, although due to how StackString is implemented s_len shouldn't ever be less than 1
}
bool equals(const char* other, bool ignore_case = false) const;
bool empty() const { return length() == 0; }
void clear() { s_clear(); }
bool operator==(const char* rhs) const { return equals(rhs); }
bool operator!=(const char* rhs) const { return !(*this == rhs); }
bool operator==(const BaseString& rhs) const { return s_len() == rhs.s_len() && *this == rhs.cstr(); }
bool operator!=(const BaseString& rhs) const { return !(*this == rhs); }
protected:
virtual void s_clear() = 0;
virtual void s_ensure(int capacity) = 0;
virtual char* s_ptr() = 0;
virtual const char* s_ptr() const = 0;
virtual int s_len() const = 0;
};
template<size_t StackSize>
class StackString final : public BaseString
{
public:
StackString() { assign(""); }
StackString(const char* cstr, const char* cstr_end = nullptr) { assign(cstr, cstr_end); }
StackString(const BaseString& other) { assign(other.cstr()); }
StackString& operator=(const BaseString& rhs)
{
assign(rhs.cstr(), rhs.cstr() + rhs.length());
return *this;
}
StackString& operator=(const char* rhs)
{
assign(rhs, nullptr);
return *this;
}
StackString& operator+=(const BaseString& rhs)
{
append(rhs.cstr());
return *this;
}
StackString& operator+=(const char* rhs)
{
append(rhs);
return *this;
}
StackString& operator+=(const char& rhs)
{
append(rhs);
return *this;
}
StackString operator+(const BaseString& rhs)
{
StackString str(*this);
str += rhs;
return str;
}
StackString operator+(const char* rhs)
{
StackString str(*this);
str += rhs;
return str;
}
StackString operator+(const char& rhs)
{
StackString str(*this);
str += rhs;
return str;
}
StackString substr(int start, int len = 0) const
{
if (len == 0) len = length() - start;
return StackString(cstr() + start, cstr() + start + len);
}
StackString trim() const
{
if (length() > 0)
{
const char* s = begin();
const char* e = end() - 1;
while (s < e && (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n' || *s == '\v' || *s == '\f')) s++;
while (e > s && (*e == ' ' || *e == '\t' || *e == '\r' || *e == '\n' || *e == '\v' || *e == '\f')) e--;
return StackString(s, e + 1);
}
return StackString();
}
Vector<StackString> split(char ch) const
{
Vector<StackString> result;
const char* ptr = s_ptr();
const char* end = ptr + length();
const char* last = ptr;
while (ptr < end)
{
if (*ptr == ch)
{
result.emplace_back(last, ptr);
last = ptr + 1;
}
ptr++;
}
if (last < ptr)
result.emplace_back(last, ptr);
return result;
}
static StackString fmt(const char* fmt, ...)
{
StackString str;
va_list args;
// determine arg m_length
va_start(args, fmt);
auto add = vsnprintf(nullptr, 0, fmt, args);
va_end(args);
if (add <= 0)
return str;
// reserve (+1 for null-terminator)
auto len = str.length();
str.s_ensure(len + add + 1);
// print out
va_start(args, fmt);
vsnprintf(str.s_ptr() + len, add + 1, fmt, args);
va_end(args);
return str;
}
protected:
void s_clear() override
{
m_stack_buffer.clear();
m_heap_buffer.clear();
s_ensure(1);
}
void s_ensure(int capacity) override
{
int count = capacity - s_len();
if (count <= 0)
return;
// expand heap buffer
if (m_heap_buffer.size() > 0)
{
m_heap_buffer.expand(count);
}
// switch from stack to heap
else if (capacity > StackSize)
{
m_heap_buffer.expand(capacity);
char* src = m_stack_buffer.data();
char* len = src + m_stack_buffer.size();
char* dst = m_heap_buffer.data();
while (src < len) *(dst++) = *(src++);
}
// expand stack buffer
else
{
m_stack_buffer.expand(count);
}
*(s_ptr() + s_len() - 1) = '\0';
}
char* s_ptr() override
{
return m_heap_buffer.size() > 0 ? m_heap_buffer.data() : m_stack_buffer.data();
}
const char* s_ptr() const override
{
return m_heap_buffer.size() > 0 ? m_heap_buffer.data() : m_stack_buffer.data();
}
int s_len() const override
{
return m_heap_buffer.size() > 0 ? m_heap_buffer.size() : m_stack_buffer.size();
}
private:
Vector<char> m_heap_buffer;
StackVector<char, StackSize> m_stack_buffer;
};
template<size_t StackSize>
StackString<StackSize> operator+(const StackString<StackSize>& lhs, const BaseString& rhs)
{
StackString str(lhs);
str += rhs;
return str;
}
template<size_t StackSize>
StackString<StackSize> operator+(const StackString<StackSize>& lhs, const char* rhs)
{
StackString str(lhs);
str += rhs;
return str;
}
template<size_t StackSize>
StackString<StackSize> operator+(const StackString<StackSize>& lhs, const char& rhs)
{
StackString str(lhs);
str += rhs;
return str;
}
// Stores enough for an empty string on the stack, and afterwards allocates on the heap
using HeapString = StackString<1>;
// Standard String with a fair amount of stack storage
using String = StackString<64>;
// Large String with enough stack storage to store FilePaths without allocating.
using FilePath = StackString<260>;
// Utf8 Utility, to iterate over a string and read utf-8 characters
struct Utf8
{
const char* str = nullptr;
u32 character = 0;
u32 character_size = 0;
Utf8() = default;
Utf8(const char* cstr);
// moves to the next character, returns false if at end of string or an invalid character
bool next();
};
struct CaseInsenstiveStringHash
{
std::size_t operator()(const Blah::BaseString& key) const
{
std::size_t result = 2166136261U;
for (auto& it : key)
{
if (it >= 'A' && it <= 'Z')
result ^= (static_cast<std::size_t>(it) - 'A' + 'a');
else
result ^= static_cast<std::size_t>(it);
result *= 16777619U;
}
return result;
}
};
struct CaseInsenstiveStringCompare
{
bool operator() (const BaseString& lhs, const BaseString& rhs) const
{
return lhs.length() == rhs.length() && lhs.starts_with(rhs, true);
}
};
}
// STD Hash Utility, so the String can be used in a set / unordered_map / map
namespace std
{
template <>
struct hash<Blah::BaseString>
{
std::size_t operator()(const Blah::BaseString& key) const
{
std::size_t result = 2166136261U;
for (auto& it : key)
{
result ^= static_cast<std::size_t>(it);
result *= 16777619U;
}
return result;
}
};
template <size_t StackSize>
struct hash<Blah::StackString<StackSize>>
{
std::size_t operator()(const Blah::StackString<StackSize>& key) const
{
std::size_t result = 2166136261U;
for (auto& it : key)
{
result ^= static_cast<std::size_t>(it);
result *= 16777619U;
}
return result;
}
};
}

View File

@ -1,6 +1,6 @@
#pragma once
#include <blah/graphics.h>
#include <blah/math/spatial.h>
#include <blah_graphics.h>
#include <blah_spatial.h>
namespace Blah
{

View File

@ -1,5 +1,5 @@
#pragma once
#include <blah/common.h>
#include <blah_common.h>
namespace Blah
{

View File

@ -1,6 +1,5 @@
#pragma once
#include <blah/common.h>
#include <string.h>
#include <blah_common.h>
namespace Blah
{
@ -182,7 +181,10 @@ namespace Blah
if constexpr (std::is_trivially_copyable<T>())
{
memcpy(new_buffer, m_buffer, m_count * sizeof(T));
u8* src = (u8*)m_buffer;
u8* len = src + m_count * sizeof(T);
u8* dst = (u8*)new_buffer;
while (src < len) *(dst++) = *(src++);
}
else
{

View File

@ -1,9 +1,9 @@
#include <blah/app.h>
#include <blah/common.h>
#include <blah/time.h>
#include "internal/internal.h"
#include "internal/platform.h"
#include "internal/renderer.h"
#include <blah_app.h>
#include <blah_common.h>
#include <blah_time.h>
#include "internal/blah_internal.h"
#include "internal/blah_platform.h"
#include "internal/blah_renderer.h"
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
@ -400,8 +400,10 @@ void App::set_flag(u32 flag, bool enabled)
if (was != app_flags)
{
Internal::platform->set_app_flags(app_flags);
Internal::renderer->set_app_flags(app_flags);
if (Internal::platform)
Internal::platform->set_app_flags(app_flags);
if (Internal::renderer)
Internal::renderer->set_app_flags(app_flags);
}
}

View File

@ -1,10 +1,10 @@
#include <blah/images/aseprite.h>
#include <blah/filesystem.h>
#include <blah/math/calc.h>
#include <blah_aseprite.h>
#include <blah_filesystem.h>
#include <blah_calc.h>
#define STBI_NO_STDIO
#define STBI_ONLY_ZLIB
#include "../third_party/stb_image.h"
#include "third_party/stb_image.h"
using namespace Blah;
@ -133,7 +133,7 @@ void Aseprite::parse_layer(Stream& stream, int frame)
layer.alpha = stream.read_u8(Endian::Little);
stream.seek(stream.position() + 3); // for future
layer.name.set_length(stream.read_u16(Endian::Little));
layer.name.append('\0', stream.read_u16(Endian::Little));
stream.read(layer.name.cstr(), layer.name.length());
layer.userdata.color = 0xffffff;
@ -261,7 +261,7 @@ void Aseprite::parse_user_data(Stream& stream, int frame)
// has text
if (flags & (1 << 0))
{
m_last_userdata->text.set_length(stream.read_u16(Endian::Little));
m_last_userdata->text.append('\0', stream.read_u16(Endian::Little));
stream.read(m_last_userdata->text.cstr(), m_last_userdata->text.length());
}
@ -287,7 +287,7 @@ void Aseprite::parse_tag(Stream& stream, int frame)
tag.color = Color(stream.read_i8(), stream.read_i8(), stream.read_i8(Endian::Little), 255);
stream.seek(stream.position() + 1);
tag.name.set_length(stream.read_u16(Endian::Little));
tag.name.append('\0', stream.read_u16(Endian::Little));
stream.read(tag.name.cstr(), tag.name.length());
tags.push_back(tag);
@ -301,7 +301,7 @@ void Aseprite::parse_slice(Stream& stream, int frame)
stream.read_u32(Endian::Little); // reserved
String name;
name.set_length(stream.read_u16(Endian::Little));
name.append('\0', stream.read_u16(Endian::Little));
stream.read(name.cstr(), name.length());
for (int s = 0; s < count; s++)

View File

@ -1,7 +1,7 @@
#include <blah/drawing/batch.h>
#include <blah/math/calc.h>
#include <blah/app.h>
#include "../internal/internal.h"
#include <blah_batch.h>
#include <blah_calc.h>
#include <blah_app.h>
#include "internal/blah_internal.h"
using namespace Blah;
@ -969,11 +969,11 @@ void Batch::str(const SpriteFont& font, const String& text, const Vec2f& pos, co
offset.y -= font.height_of(text) * justify.y;
u32 last = 0;
for (int i = 0, l = text.length(); i < l; i += text.utf8_length(i))
Utf8 utf8(text.cstr());
int i = 0;
while (utf8.character)
{
u32 next = text.utf8_at(i);
if (next == '\n')
if (utf8.character == '\n')
{
offset.x = 0;
offset.y += font.line_height();
@ -982,22 +982,24 @@ void Batch::str(const SpriteFont& font, const String& text, const Vec2f& pos, co
offset.x -= font.width_of_line(text, i + 1) * justify.x;
last = 0;
continue;
}
const auto& ch = font[next];
if (ch.subtexture.texture)
else
{
Vec2f at = offset + ch.offset;
const auto& ch = font[utf8.character];
if (ch.subtexture.texture)
{
Vec2f at = offset + ch.offset;
if (last)
at.x += font.get_kerning(last, utf8.character);
tex(ch.subtexture, at, color);
}
if (i > 0 && text[i - 1] != '\n')
at.x += font.get_kerning(last, next);
tex(ch.subtexture, at, color);
offset.x += ch.advance;
last = utf8.character;
}
offset.x += ch.advance;
last = next;
i += utf8.character_size;
utf8.next();
}
pop_matrix();

View File

@ -1,7 +1,5 @@
#include <blah/common.h>
#include <blah/app.h>
#include <stdarg.h> // for logging methods
#include <stdio.h> // for sprintf
#include <blah_common.h>
#include <blah_app.h>
using namespace Blah;

View File

@ -1,5 +1,5 @@
#include <blah/filesystem.h>
#include "internal/internal.h"
#include <blah_filesystem.h>
#include "internal/blah_internal.h"
using namespace Blah;
@ -70,7 +70,10 @@ Vector<FilePath> Directory::enumerate(const FilePath& path, bool recursive)
{
App::Internal::platform->dir_enumerate(list, path.cstr(), recursive);
for (auto& it : list)
it.replace('\\', '/');
{
for (int n = 0; n < it.length(); n ++)
if (it[n] == '\\') it[n] = '/';
}
}
return list;
@ -114,10 +117,10 @@ FilePath Path::get_directory_name(const FilePath& path)
{
FilePath directory = path;
while (directory.ends_with("/"))
directory = directory.substr(0, -1);
directory = FilePath(directory.begin(), directory.end() - 1);
auto last = directory.last_index_of('/');
if (last >= 0)
directory = directory.substr(0, last + 1);
directory = FilePath(directory.begin(), directory.begin() + last + 1);
return directory;
}
@ -156,7 +159,7 @@ FilePath Path::normalize(const FilePath& path)
for (auto k = normalized.length() - 1; k > 0; k--)
if (normalized[k - 1] == '/')
{
normalized = normalized.substr(0, k - 1);
normalized = FilePath(normalized.begin(), normalized.begin() + k - 1);
could_move_up = true;
break;
}
@ -181,5 +184,5 @@ FilePath Path::join(const FilePath& a, const FilePath& b)
else if (b.length() <= 0)
return normalize(a);
else
return normalize(FilePath(a).append("/").append(b));
return normalize(a + "/" + b);
}

View File

@ -1,15 +1,15 @@
#include <blah/images/font.h>
#include <blah/math/calc.h>
#include <blah_font.h>
#include <blah_calc.h>
using namespace Blah;
#define STBTT_STATIC
#define STB_TRUETYPE_IMPLEMENTATION
#include "../third_party/stb_truetype.h"
#include "third_party/stb_truetype.h"
namespace
{
String get_font_name(stbtt_fontinfo* font, int nameId)
String blah_get_font_name(stbtt_fontinfo* font, int name_id)
{
int length = 0;
@ -18,14 +18,14 @@ namespace
STBTT_PLATFORM_ID_MICROSOFT,
STBTT_MS_EID_UNICODE_BMP,
STBTT_MS_LANG_ENGLISH,
nameId);
name_id);
// we want the size in wide chars
length /= 2;
String str;
if (length > 0)
str.append_utf16(ptr, ptr + length, Calc::is_little_endian());
str.append(ptr, ptr + length, Calc::is_little_endian());
return str;
}
}
@ -56,8 +56,8 @@ FontRef Font::create(Stream& stream)
auto font = FontRef(new Font());
font->m_font = stbtt;
font->m_buffer = std::move(buffer);
font->m_family_name = get_font_name(fn, 1);
font->m_style_name = get_font_name(fn, 2);
font->m_family_name = blah_get_font_name(fn, 1);
font->m_style_name = blah_get_font_name(fn, 2);
stbtt_GetFontVMetrics(fn, &font->m_ascent, &font->m_descent, &font->m_line_gap);
return font;
}

View File

@ -1,5 +1,6 @@
#include <blah/graphics.h>
#include "internal/internal.h"
#include <blah_graphics.h>
#include <blah_stream.h>
#include "internal/blah_internal.h"
using namespace Blah;

View File

@ -1,14 +1,14 @@
#include <blah/images/image.h>
#include <blah_image.h>
using namespace Blah;
#define STB_IMAGE_IMPLEMENTATION
#define STBI_ONLY_JPEG
#define STBI_ONLY_PNG
#include "../third_party/stb_image.h"
#include "third_party/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "../third_party/stb_image_write.h"
#include "third_party/stb_image_write.h"
namespace
{

View File

@ -1,11 +1,10 @@
#include <blah/input.h>
#include <blah/app.h>
#include <blah/time.h>
#include <blah/common.h>
#include <blah/math/calc.h>
#include "internal/internal.h"
#include "internal/platform.h"
#include <cstring>
#include <blah_input.h>
#include <blah_app.h>
#include <blah_time.h>
#include <blah_common.h>
#include <blah_calc.h>
#include "internal/blah_internal.h"
#include "internal/blah_platform.h"
using namespace Blah;
@ -33,9 +32,9 @@ void Input::Internal::init()
Input::last_state = g_empty_state;
Input::state = g_empty_state;
g_buttons.dispose();
g_axes.dispose();
g_sticks.dispose();
g_buttons = Vector<Ref<ButtonBinding>>();
g_axes = Vector<Ref<AxisBinding>>();
g_sticks = Vector<Ref<StickBinding>>();
}
void Input::Internal::shutdown()

View File

@ -1,6 +1,5 @@
#include <blah/images/packer.h>
#include <blah_packer.h>
#include <algorithm>
#include <cstring>
using namespace Blah;

View File

@ -1,5 +1,5 @@
#include <blah/drawing/spritefont.h>
#include <blah/images/packer.h>
#include <blah_spritefont.h>
#include <blah_packer.h>
using namespace Blah;
@ -46,21 +46,25 @@ float SpriteFont::width_of(const String& text) const
float line_width = 0;
Codepoint last = 0;
for (int i = 0; i < text.length(); i += text.utf8_length(i))
Utf8 utf8(text.cstr());
while (utf8.character)
{
if (text[i] == '\n')
if (utf8.character == '\n')
{
line_width = 0;
continue;
}
else
{
line_width += get_character(utf8.character).advance;
if (last)
line_width += get_kerning(last, utf8.character);
if (line_width > width)
width = line_width;
last = utf8.character;
}
auto next = text.utf8_at(i);
line_width += get_character(next).advance;
if (i > 0)
line_width += get_kerning(last, next);
if (line_width > width)
width = line_width;
last = next;
utf8.next();
}
return width;
@ -72,18 +76,19 @@ float SpriteFont::width_of_line(const String& text, int start) const
if (start >= text.length()) return 0;
float width = 0;
Codepoint last = 0;
for (auto i = start; i < text.length(); i += text.utf8_length(i))
{
if (text[i] == '\n')
return width;
auto next = text.utf8_at(i);
width += get_character(next).advance;
if (i > 0)
width += get_kerning(last, next);
last = next;
Utf8 utf8(text.cstr());
while (utf8.character)
{
if (utf8.character == '\n')
break;
width += get_character(utf8.character).advance;
if (last)
width += get_kerning(last, utf8.character);
last = utf8.character;
utf8.next();
}
return width;
@ -95,10 +100,13 @@ float SpriteFont::height_of(const String& text) const
return 0;
float height = line_height();
for (auto i = 0; i < text.length(); i += text.utf8_length(i))
Utf8 utf8(text.cstr());
while (utf8.character)
{
if (text[i] == '\n')
if (utf8.character == '\n')
height += line_height();
utf8.next();
}
return height - line_gap;

View File

@ -1,6 +1,5 @@
#include <blah/stream.h>
#include <blah/containers/str.h>
#include <string.h>
#include <blah_stream.h>
#include <blah_string.h>
using namespace Blah;
@ -47,7 +46,7 @@ String Stream::read_string(int length)
}
else
{
result.set_length(length);
result.append('\0', length);
read(result.cstr(), length);
}

360
src/blah_string.cpp Normal file
View File

@ -0,0 +1,360 @@
#include <blah_string.h>
using namespace Blah;
namespace
{
char blah_to_lower(char c)
{
if (c >= 'A' && c <= 'Z') return c - 'A' + 'a';
return c;
}
int blah_strlen(const char* cstr)
{
int len = 0;
if (cstr)
while (*(cstr + len) != '\0' && len < INT32_MAX) len++;
return len;
}
}
void BaseString::assign(const char* cstr, const char* cstr_end)
{
s_clear();
append(cstr, cstr_end);
}
void BaseString::append(const char* cstr, const char* cstr_end)
{
// make sure values are valid
if (cstr == nullptr || *cstr == '\0')
return;
if (cstr_end == nullptr)
cstr_end = cstr + blah_strlen(cstr);
// reserve (+1 for null-terminator)
auto len = length();
s_ensure(len + (cstr_end - cstr) + 1);
// copy value over to our buffer
char* dst = s_ptr() + len;
while (cstr < cstr_end)
*(dst++) = *(cstr++);
}
void BaseString::append(const u16* u16_cstr, const u16* u16_cstr_end, bool swap_endian)
{
// converts utf16 into utf8
// more info: https://en.wikipedia.org/wiki/UTF-16#Description
const u16 surrogate_min = 0xd800u;
const u16 surrogate_max = 0xdbffu;
while ((u16_cstr_end == nullptr && *u16_cstr != 0) || (u16_cstr_end != nullptr && u16_cstr != u16_cstr_end))
{
u16 next = (*u16_cstr++);
if (swap_endian)
next = ((next & 0xff) << 8 | ((next & 0xff00) >> 8));
u32 cp = 0xffff & next;
if ((cp >= surrogate_min && cp <= surrogate_max))
{
next = (*u16_cstr++);
if (swap_endian)
next = ((next & 0xff) << 8 | ((next & 0xff00) >> 8));
u32 trail = 0xffff & next;
cp = (cp << 10) + trail + 0x10000u - (surrogate_min << 10) - 0xdc00u;
}
append(cp);
}
}
void BaseString::append(char ch, int count)
{
// reserve (+1 for null-terminator)
auto len = length();
s_ensure(len + count + 1);
// copy value over to our buffer
char* dst = s_ptr() + len;
char* end = dst + count;
while (dst < end)
*(dst++) = ch;
}
void BaseString::append(u32 c)
{
// one octet
if (c < 0x80)
{
append((char)c);
}
// two octets
else if (c < 0x800)
{
append((char)((c >> 6) | 0xc0));
append((char)((c & 0x3f) | 0x80));
}
// three octets
else if (c < 0x10000)
{
append((char)((c >> 12) | 0xe0));
append((char)(((c >> 6) & 0x3f) | 0x80));
append((char)((c & 0x3f) | 0x80));
}
// four octets
else
{
append((char)((c >> 18) | 0xf0));
append((char)(((c >> 12) & 0x3f) | 0x80));
append((char)(((c >> 6) & 0x3f) | 0x80));
append((char)((c & 0x3f) | 0x80));
}
}
void BaseString::append_fmt(const char* fmt, ...)
{
va_list args;
// determine arg m_length
va_start(args, fmt);
auto add = vsnprintf(nullptr, 0, fmt, args);
va_end(args);
if (add <= 0)
return;
// reserve (+1 for null-terminator)
auto len = length();
s_ensure(len + add + 1);
// print out
va_start(args, fmt);
vsnprintf(s_ptr() + len, add + 1, fmt, args);
va_end(args);
}
bool BaseString::starts_with(const char* cstr, bool ignore_case) const
{
if (cstr == nullptr || *cstr == '\0')
return length() == 0;
const char* ptr = s_ptr();
if (ignore_case)
{
while (*cstr != '\0')
{
if (blah_to_lower(*cstr) != blah_to_lower(*ptr))
return false;
cstr++; ptr++;
}
}
else
{
while (*cstr != '\0')
{
if (*cstr != *ptr)
return false;
cstr++; ptr++;
}
}
return true;
}
bool BaseString::ends_with(const char* cstr, bool ignore_case) const
{
if (cstr == nullptr || *cstr == '\0')
return length() == 0;
int len = blah_strlen(cstr);
if (len > length())
return false;
const char* ptr = s_ptr() + length() - len;
if (ignore_case)
{
while (*cstr != '\0')
{
if (blah_to_lower(*cstr) != blah_to_lower(*ptr))
return false;
cstr++; ptr++;
}
}
else
{
while (*cstr != '\0')
{
if (*cstr != *ptr)
return false;
cstr++; ptr++;
}
}
return true;
}
bool BaseString::contains(const char* cstr, bool ignore_case) const
{
if (cstr == nullptr || *cstr == '\0')
return length() == 0;
int len = blah_strlen(cstr);
if (len > length())
return false;
const char* ptr = s_ptr();
const char* end = s_ptr() + len;
while (ptr < end)
{
const char* at = ptr;
bool match = true;
if (ignore_case)
{
while (*cstr != '\0')
{
if (blah_to_lower(*cstr) != blah_to_lower(*at))
{
match = false;
break;
}
cstr++; at++;
}
}
else
{
while (*cstr != '\0')
{
if (*cstr != *at)
{
match = false;
break;
}
cstr++; at++;
}
}
if (match)
return true;
}
return false;
}
int BaseString::first_index_of(char ch) const
{
const char* ptr = s_ptr();
const char* end = ptr + length();
while (ptr < end)
{
if (*ptr == ch)
return ptr - s_ptr();
ptr++;
}
return -1;
}
int BaseString::last_index_of(char ch) const
{
const char* ptr = s_ptr();
const char* end = ptr + length() - 1;
while (end >= ptr)
{
if (*end == ch)
return end - s_ptr();
end--;
}
return -1;
}
bool BaseString::equals(const char* other, bool ignore_case) const
{
const char* a = s_ptr(); const char* b = other;
if (ignore_case)
{
while (blah_to_lower(*a) == blah_to_lower(*b) && *a != '\0') { a++; b++; }
return blah_to_lower(*a) == blah_to_lower(*b);
}
else
{
while (*a == *b && *a != '\0') { a++; b++; }
return *a == *b;
}
}
namespace
{
u32 utf8_character_at(const char* str)
{
u32 charcode = 0;
int t = (unsigned char)(*(str++));
if (t < 128)
return t;
int high_bit_mask = (1 << 6) - 1;
int high_bit_shift = 0;
int total_bits = 0;
int other_bits = 6;
while ((t & 0xC0) == 0xC0)
{
t <<= 1;
t &= 0xff;
total_bits += 6;
high_bit_mask >>= 1;
high_bit_shift++;
charcode <<= other_bits;
charcode |= ((unsigned char)(*(str++))) & ((1 << other_bits) - 1);
}
charcode |= ((t >> high_bit_shift) & high_bit_mask) << total_bits;
return charcode;
}
u32 utf8_character_size(const char* str)
{
char c = *str;
if (c == '\0')
return 0;
if ((c & 0xFE) == 0xFC)
return 6;
if ((c & 0xFC) == 0xF8)
return 5;
if ((c & 0xF8) == 0xF0)
return 4;
if ((c & 0xF0) == 0xE0)
return 3;
if ((c & 0xE0) == 0xC0)
return 2;
return 1;
}
}
Utf8::Utf8(const char* cstr)
: str(cstr)
{
character = utf8_character_at(str);
character_size = utf8_character_size(str);
}
bool Utf8::next()
{
str += character_size;
character = utf8_character_at(str);
character_size = utf8_character_size(str);
return character_size > 0;
}

View File

@ -1,5 +1,5 @@
#include <blah/drawing/subtexture.h>
#include <blah/math/calc.h>
#include <blah_subtexture.h>
#include <blah_calc.h>
using namespace Blah;

View File

@ -1,5 +1,5 @@
#include <blah/time.h>
#include "internal/internal.h"
#include <blah_time.h>
#include "internal/blah_internal.h"
using namespace Blah;

View File

@ -1,492 +0,0 @@
#include <blah/containers/str.h>
#include <string.h> // for strcpy etc
#include <stdarg.h> // for format methods
#include <stdio.h> // for sprintf
#include <cctype> // toupper
using namespace Blah;
char Str::empty_buffer[1] = { '\0' };
bool Str::operator==(const Str& rhs) const
{
return strcmp(cstr(), rhs.cstr()) == 0;
}
bool Str::operator!=(const Str& rhs) const
{
return strcmp(cstr(), rhs.cstr()) != 0;
}
bool Str::operator==(const char* rhs) const
{
return strcmp(cstr(), rhs) == 0;
}
bool Str::operator!=(const char* rhs) const
{
return strcmp(cstr(), rhs) != 0;
}
void Str::reserve(int size)
{
int buffer_length = size + 1;
if (buffer_length > m_capacity)
{
if (m_capacity <= 0)
m_capacity = 16;
while (m_capacity < buffer_length)
m_capacity *= 2;
// expand from local buffer
if (m_buffer == nullptr)
{
char* local = data();
m_buffer = new char[m_capacity];
memcpy(m_buffer, local, m_local_size);
m_buffer[m_local_size] = '\0';
}
// expand from empty buffer
else if (m_buffer == empty_buffer)
{
m_buffer = new char[m_capacity];
m_buffer[0] = '\0';
}
// expand from existing heap buffer
else
{
char* new_buffer = new char[m_capacity];
memcpy(new_buffer, m_buffer, m_length);
new_buffer[m_length] = '\0';
delete[] m_buffer;
m_buffer = new_buffer;
}
}
}
void Str::set_length(int len)
{
reserve(len);
data()[len] = '\0';
m_length = len;
}
u32 Str::utf8_at(int index) const
{
u32 charcode = 0;
int t = (unsigned char)(this->operator[](index++));
if (t < 128)
return t;
int high_bit_mask = (1 << 6) - 1;
int high_bit_shift = 0;
int total_bits = 0;
int other_bits = 6;
while ((t & 0xC0) == 0xC0)
{
t <<= 1;
t &= 0xff;
total_bits += 6;
high_bit_mask >>= 1;
high_bit_shift++;
charcode <<= other_bits;
charcode |= ((unsigned char)(this->operator[](index++))) & ((1 << other_bits) - 1);
}
charcode |= ((t >> high_bit_shift) & high_bit_mask) << total_bits;
return charcode;
}
int Str::utf8_length(int index) const
{
auto c = this->operator[](index);
if ((c & 0xFE) == 0xFC)
return 6;
if ((c & 0xFC) == 0xF8)
return 5;
if ((c & 0xF8) == 0xF0)
return 4;
else if ((c & 0xF0) == 0xE0)
return 3;
else if ((c & 0xE0) == 0xC0)
return 2;
return 1;
}
Str& Str::append(char c)
{
reserve(m_length + 1);
data()[m_length++] = c;
data()[m_length] = '\0';
return *this;
}
Str& Str::append(u32 c)
{
// one octet
if (c < 0x80)
{
append((char)c);
}
// two octets
else if (c < 0x800)
{
append((char)((c >> 6) | 0xc0));
append((char)((c & 0x3f) | 0x80));
}
// three octets
else if (c < 0x10000)
{
append((char)((c >> 12) | 0xe0));
append((char)(((c >> 6) & 0x3f) | 0x80));
append((char)((c & 0x3f) | 0x80));
}
// four octets
else
{
append((char)((c >> 18) | 0xf0));
append((char)(((c >> 12) & 0x3f) | 0x80));
append((char)(((c >> 6) & 0x3f) | 0x80));
append((char)((c & 0x3f) | 0x80));
}
return *this;
}
Str& Str::append(const char* start, const char* end)
{
if (end == nullptr)
end = start + strlen(start);
int add = (int)(end - start);
if (add <= 0)
return *this;
reserve(m_length + add);
memcpy(data() + m_length, start, add);
m_length += add;
data()[m_length] = '\0';
return *this;
}
Str& Str::append(const Str& str, int start, int end)
{
if (end < 0) end = str.m_length;
if (end > str.m_length) end = str.m_length;
if (start < 0) start = 0;
if (start > end) start = end;
return append(str.begin() + start, str.begin() + end);
}
Str& Str::append_fmt(const char* fmt, ...)
{
int add, diff;
// determine arg m_length
va_list args;
va_start(args, fmt);
add = vsnprintf(NULL, 0, fmt, args);
va_end(args);
// reserve
reserve(m_length + add);
diff = m_capacity - m_length;
if (diff <= 0) diff = 0;
// print out
va_start(args, fmt);
vsnprintf(data() + m_length, (size_t)diff, fmt, args);
va_end(args);
// increment size
m_length += add;
data()[m_length] = '\0';
return *this;
}
Str& Str::append_utf16(const u16* start, const u16* end, bool swap_endian)
{
// converts utf16 into utf8
// more info: https://en.wikipedia.org/wiki/UTF-16#Description
const u16 surrogate_min = 0xd800u;
const u16 surrogate_max = 0xdbffu;
while ((end == nullptr && *start != 0) || (end != nullptr && start != end))
{
u16 next = (*start++);
if (swap_endian)
next = ((next & 0xff) << 8 | ((next & 0xff00) >> 8));
u32 cp = 0xffff & next;
if ((cp >= surrogate_min && cp <= surrogate_max))
{
next = (*start++);
if (swap_endian)
next = ((next & 0xff) << 8 | ((next & 0xff00) >> 8));
u32 trail = 0xffff & next;
cp = (cp << 10) + trail + 0x10000u - (surrogate_min << 10) - 0xdc00u;
}
append(cp);
}
return *this;
}
Str& Str::trim()
{
if (m_length > 0)
{
const char* s = begin();
const char* e = end() - 1;
while (isspace(*s) && s != e)
s++;
while (isspace(*e) && s != e)
e--;
set(s, e + 1);
}
return *this;
}
bool Str::starts_with(const char* str, bool ignoreCase) const
{
if (str == nullptr)
return m_length == 0;
int len = (int)strlen(str);
if (len > m_length)
return false;
const char* a = data();
const char* b = str;
if (ignoreCase)
{
for (int n = 0; n < len; n++)
if (tolower(a[n]) != tolower(b[n]))
return false;
}
else
{
for (int n = 0; n < len; n++)
if (a[n] != b[n])
return false;
}
return true;
}
bool Str::contains(const char* str, bool ignoreCase) const
{
int len = (int)strlen(str);
if (len > m_length || len <= 0)
return false;
const char* a = data();
const char* b = str;
for (int start = 0; start < m_length - len + 1; start++)
{
bool match = true;
if (ignoreCase)
{
for (int n = 0; n < len && match; n++)
if (tolower(a[start + n]) != tolower(b[n]))
match = false;
}
else
{
for (int n = 0; n < len && match; n++)
if (a[start + n] != b[n])
match = false;
}
if (match)
return true;
}
return false;
}
bool Str::ends_with(const char* str, bool ignoreCase) const
{
if (str == nullptr)
return m_length == 0;
int len = (int)strlen(str);
if (len > m_length || len <= 0)
return false;
const char* a = data();
const char* b = str;
if (ignoreCase)
{
for (int n = m_length - len, i = 0; n < m_length; n++, i++)
if (tolower(a[n]) != tolower(b[i]))
return false;
}
else
{
for (int n = m_length - len, i = 0; n < m_length; n++, i++)
if (a[n] != b[i])
return false;
}
return true;
}
int Str::first_index_of(char ch) const
{
const char* ptr = data();
for (int n = 0; n < m_length; n++)
if (ptr[n] == ch)
return n;
return -1;
}
int Str::last_index_of(char ch) const
{
const char* ptr = data();
for (int n = m_length - 1; n >= 0; n--)
if (ptr[n] == ch)
return n;
return -1;
}
String Str::substr(int start) const
{
if (start < 0) start = 0;
if (start > m_length) start = m_length;
return String(data() + start);
}
String Str::substr(int start, int end) const
{
if (start < 0) start = 0;
if (start > m_length) start = m_length;
if (end < 0) end = m_length + end;
if (end < start) end = start;
if (end > m_length) end = m_length;
return Str(data() + start, data() + end);
}
Vector<String> Str::split(char ch) const
{
Vector<String> result;
const char* ptr = data();
int last = 0;
int index = 1;
while (index < m_length)
{
if (ptr[index] == ch)
{
result.push_back(substr(last, index));
last = index + 1;
}
index++;
}
if (last < index)
result.push_back(substr(last, index));
return result;
}
Str& Str::replace(const Str& os, const Str& ns)
{
for (int i = 0; i < m_length - os.m_length + 1; i++)
{
if (strncmp(data() + i, os.data(), os.m_length) == 0)
{
if (ns.m_length > os.m_length)
reserve(ns.m_length - os.m_length);
memmove(data() + i + ns.m_length, data() + i + os.m_length, m_length - i - os.m_length);
memcpy(data() + i, ns.cstr(), ns.m_length);
set_length(m_length + ns.m_length - os.m_length);
i += os.m_length - 1;
}
}
return *this;
}
Str& Str::replace(char c, char r)
{
char* ptr = data();
for (int n = 0; n < m_length; n++)
if (ptr[n] == c)
ptr[n] = r;
return *this;
}
void Str::clear()
{
if (m_capacity > 0)
data()[0] = '\0';
m_length = 0;
}
void Str::dispose()
{
if (m_buffer != nullptr && m_buffer != empty_buffer)
delete[] m_buffer;
if (m_local_size > 0)
{
m_buffer = nullptr;
m_capacity = m_local_size;
data()[0] = '\0';
}
else
{
m_buffer = empty_buffer;
m_capacity = 0;
}
m_length = 0;
}
void Str::set(const char* start, const char* end)
{
// find the end
if (end == nullptr)
end = start + strlen(start);
// make sure it actually contains characters
int len = (int)(end - start);
if (len <= 0)
{
clear();
}
else
{
m_length = len;
// reserve
reserve(m_length);
// copy the data over
char* ptr = data();
memcpy(ptr, start, m_length);
ptr[m_length] = '\0';
}
}

View File

@ -1,6 +1,6 @@
#pragma once
#include "renderer.h"
#include "platform.h"
#include "blah_renderer.h"
#include "blah_platform.h"
#define BLAH_ASSERT_RENDERER() BLAH_ASSERT(App::Internal::renderer, "Renderer has not been created")
#define BLAH_ASSERT_PLATFORM() BLAH_ASSERT(App::Internal::platform, "Platform has not been created")

View File

@ -1,8 +1,8 @@
#pragma once
#include <blah/common.h>
#include <blah/input.h>
#include <blah/filesystem.h>
#include <blah/containers/vector.h>
#include <blah_common.h>
#include <blah_input.h>
#include <blah_filesystem.h>
#include <blah_vector.h>
namespace Blah
{

View File

@ -1,13 +1,13 @@
#ifdef BLAH_PLATFORM_SDL2
#include "platform.h"
#include "renderer.h"
#include "internal.h"
#include <blah/input.h>
#include <blah/app.h>
#include <blah/filesystem.h>
#include <blah/common.h>
#include <blah/time.h>
#include "blah_platform.h"
#include "blah_renderer.h"
#include "blah_internal.h"
#include <blah_input.h>
#include <blah_app.h>
#include <blah_filesystem.h>
#include <blah_common.h>
#include <blah_time.h>
#include <SDL.h>

View File

@ -3,13 +3,13 @@
// Note:
// This is unfinished! It is missing Controller Support!
#include "platform.h"
#include "internal.h"
#include <blah/input.h>
#include <blah/app.h>
#include <blah/filesystem.h>
#include <blah/common.h>
#include <blah/time.h>
#include "blah_platform.h"
#include "blah_internal.h"
#include <blah_input.h>
#include <blah_app.h>
#include <blah_filesystem.h>
#include <blah_common.h>
#include <blah_time.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

View File

@ -1,7 +1,7 @@
#pragma once
#include <blah/app.h>
#include <blah/graphics.h>
#include <blah/math/color.h>
#include <blah_app.h>
#include <blah_graphics.h>
#include <blah_color.h>
namespace Blah
{

View File

@ -3,10 +3,10 @@
// TODO:
// Note the D3D11 Implementation is still a work-in-progress
#include "renderer.h"
#include "internal.h"
#include "platform.h"
#include <blah/common.h>
#include "blah_renderer.h"
#include "blah_internal.h"
#include "blah_platform.h"
#include <blah_common.h>
#include <cstdio>
#include <cstring>
#include <cstddef>

View File

@ -1,9 +1,9 @@
#ifdef BLAH_RENDERER_OPENGL
#include "renderer.h"
#include "internal.h"
#include "platform.h"
#include <blah/common.h>
#include "blah_renderer.h"
#include "blah_internal.h"
#include "blah_platform.h"
#include <blah_common.h>
#include <stdio.h>
#include <string.h>
#include <stddef.h>
@ -942,7 +942,7 @@ namespace Blah
m_uniforms.push_back(tex_uniform);
UniformInfo sampler_uniform;
sampler_uniform.name = String(name).append("_sampler");
sampler_uniform.name = String(name) + "_sampler";
sampler_uniform.register_index = sampler_uniforms;
sampler_uniform.buffer_index = 0;
sampler_uniform.array_length = size;