Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a dedicated class for easy load padded string for simdjson parser
Browse files Browse the repository at this point in the history
halx99 committed Oct 11, 2023
1 parent 8b18f31 commit e5326e7
Showing 4 changed files with 109 additions and 18 deletions.
22 changes: 4 additions & 18 deletions core/2d/FontAtlas.cpp
Original file line number Diff line number Diff line change
@@ -43,6 +43,8 @@
#include "fmt/format.h"
#include "base/ZipUtils.h"

#include "base/PaddedString.h"

NS_AX_BEGIN

const int FontAtlas::CacheTextureWidth = 512;
@@ -54,27 +56,11 @@ void FontAtlas::loadFontAtlas(std::string_view fontatlasFile, hlookup::string_ma
{
using namespace simdjson;

struct PaddingString : protected yasio::sbyte_buffer
{
public:
using value_type = yasio::sbyte_buffer::value_type;
size_t size() const { return this->yasio::sbyte_buffer::size(); }
void resize(size_t size)
{
this->yasio::sbyte_buffer::resize(size + SIMDJSON_PADDING);
this->yasio::sbyte_buffer::data()[size] = '\0';
}

char* data() { return this->yasio::sbyte_buffer::data(); };
};

try
{
PaddingString strJson;
FileUtils::getInstance()->getContents(fontatlasFile, &strJson);
auto strJson = PaddedString::load(fontatlasFile);
ondemand::parser parser;
padded_string_view json(strJson.data(), strJson.size());
ondemand::document settings = parser.iterate(json);
ondemand::document settings = parser.iterate(strJson);
std::string_view type = settings["type"];
if (type != "fontatlas")
{
2 changes: 2 additions & 0 deletions core/base/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -80,6 +80,8 @@ set(_AX_BASE_HEADER
base/Scheduler.h
base/EventType.h
base/IMEDispatcher.h
base/PaddedString.h
base/JsonWriter.h
)

set(_AX_BASE_SRC
3 changes: 3 additions & 0 deletions core/base/JsonWriter.h
Original file line number Diff line number Diff line change
@@ -21,6 +21,9 @@
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
A simple wrapper dotnet Utf8JsonWriter like API style of rapidjson
****************************************************************************/

#pragma once
100 changes: 100 additions & 0 deletions core/base/PaddedString.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/****************************************************************************
Copyright (c) 2023 HALX99.
https://axmolengine.github.io/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

#pragma once

#include "simdjson/simdjson.h"
#include "platform/FileUtils.h"

NS_AX_BEGIN

/**
* @addtogroup base
* @{
*/

/**
@brief A dedicated class for easy load padded string for simdjson parser.
The only legal use case is:
auto strJson = PaddedString::load(fontatlasFile);
ondemand::parser parser;
ondemand::document settings = parser.iterate(strJson);
*/
class PaddedString final
{
public:
static PaddedString load(std::string_view filename)
{
PaddedString ret;
FileUtils::getInstance()->getContents(filename, &ret);
return ret;
}

PaddedString() = default;
~PaddedString() { delete[] data_ptr; }

PaddedString(PaddedString&& o) noexcept : viable_size(o.viable_size), data_ptr(o.data_ptr)
{
o.data_ptr = nullptr; // we take ownership
}
PaddedString& operator=(PaddedString&& o) noexcept
{
swap(o);
return *this;
}
void swap(PaddedString& o) noexcept
{
std::swap(viable_size, o.viable_size);
std::swap(data_ptr, o.data_ptr);
}

using value_type = char;
size_t size() const { return viable_size; }
void resize(size_t size)
{
assert(!data_ptr);
data_ptr = simdjson::internal::allocate_padded_buffer(size);
viable_size = size;
}

char* data() { return data_ptr; }
const char* data() const { return data_ptr; }

operator simdjson::padded_string_view() const noexcept
{
return simdjson::padded_string_view(data(), size(), size() + simdjson::SIMDJSON_PADDING);
}

private:
PaddedString(const PaddedString&) = delete;
size_t viable_size{0};
char* data_ptr{nullptr};
};

NS_AX_END

0 comments on commit e5326e7

Please sign in to comment.