String buffer
parent
2ad00c8895
commit
d06c7ebeb6
|
@ -9,6 +9,8 @@ option(BUILD_NBT "Build the BLT NBT + eNBT extension" ON)
|
||||||
option(BUILD_TESTS "Build the BLT test set" OFF)
|
option(BUILD_TESTS "Build the BLT test set" OFF)
|
||||||
option(BLT_ENABLE_LOGGING "Enable blt::logging" ON)
|
option(BLT_ENABLE_LOGGING "Enable blt::logging" ON)
|
||||||
|
|
||||||
|
configure_file(include/blt/config.h.in config/blt/config.h @ONLY)
|
||||||
|
|
||||||
if(${BUILD_STD} OR ${BUILD_PROFILING})
|
if(${BUILD_STD} OR ${BUILD_PROFILING})
|
||||||
file(GLOB_RECURSE STD_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/blt/std/*.cpp")
|
file(GLOB_RECURSE STD_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/blt/std/*.cpp")
|
||||||
else()
|
else()
|
||||||
|
@ -44,8 +46,6 @@ message("Profiler Files ${PROFILING_FILES}")
|
||||||
message("Source: ${CMAKE_SOURCE_DIR}")
|
message("Source: ${CMAKE_SOURCE_DIR}")
|
||||||
message("Current Source: ${CMAKE_CURRENT_SOURCE_DIR}")
|
message("Current Source: ${CMAKE_CURRENT_SOURCE_DIR}")
|
||||||
|
|
||||||
configure_file(include/blt/config.h.in config/blt/config.h @ONLY)
|
|
||||||
|
|
||||||
add_library(BLT ${STD_FILES} ${PROFILING_FILES} ${NBT_FILES})
|
add_library(BLT ${STD_FILES} ${PROFILING_FILES} ${NBT_FILES})
|
||||||
|
|
||||||
target_include_directories(BLT PUBLIC include/)
|
target_include_directories(BLT PUBLIC include/)
|
||||||
|
|
Before Width: | Height: | Size: 39 KiB After Width: | Height: | Size: 39 KiB |
Before Width: | Height: | Size: 118 KiB After Width: | Height: | Size: 118 KiB |
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 12 KiB |
|
@ -16,6 +16,30 @@
|
||||||
|
|
||||||
namespace blt::string {
|
namespace blt::string {
|
||||||
|
|
||||||
|
class StringBuffer {
|
||||||
|
private:
|
||||||
|
const size_t BLOCK_SIZE = 4096;
|
||||||
|
size_t front = 0;
|
||||||
|
size_t size = 0;
|
||||||
|
char* characterBuffer = nullptr;
|
||||||
|
|
||||||
|
void expand();
|
||||||
|
public:
|
||||||
|
void trim();
|
||||||
|
std::string toString();
|
||||||
|
|
||||||
|
StringBuffer(){
|
||||||
|
characterBuffer = static_cast<char*>(malloc(BLOCK_SIZE));
|
||||||
|
size = BLOCK_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuffer& operator<<(char c);
|
||||||
|
|
||||||
|
~StringBuffer() {
|
||||||
|
free(characterBuffer);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
static inline bool starts_with(const std::string& string, const std::string& search){
|
static inline bool starts_with(const std::string& string, const std::string& search){
|
||||||
if (search.length() > string.length())
|
if (search.length() > string.length())
|
||||||
return false;
|
return false;
|
||||||
|
@ -153,6 +177,7 @@ namespace blt::string {
|
||||||
trim(s);
|
trim(s);
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif //BLT_STRING_H
|
#endif //BLT_STRING_H
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
//
|
||||||
|
// Created by brett on 7/9/23.
|
||||||
|
//
|
||||||
|
#include <blt/std/string.h>
|
||||||
|
|
||||||
|
void blt::string::StringBuffer::expand() {
|
||||||
|
size_t multiplier = size / BLOCK_SIZE;
|
||||||
|
auto newSize = BLOCK_SIZE * (multiplier * 2);
|
||||||
|
characterBuffer = static_cast<char*>(realloc(characterBuffer, newSize));
|
||||||
|
size = newSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
void blt::string::StringBuffer::trim() {
|
||||||
|
characterBuffer = static_cast<char*>(realloc(characterBuffer, front+1));
|
||||||
|
size = front+1;
|
||||||
|
characterBuffer[front] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
blt::string::StringBuffer& blt::string::StringBuffer::operator<<(char c) {
|
||||||
|
characterBuffer[front++] = c;
|
||||||
|
if (front > size)
|
||||||
|
expand();
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string blt::string::StringBuffer::toString() {
|
||||||
|
trim();
|
||||||
|
return std::string{characterBuffer};
|
||||||
|
}
|
Loading…
Reference in New Issue