Skip to content

Commit

Permalink
从 ryu 切换到 google.double-conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
9chu committed Feb 15, 2024
1 parent 438e190 commit 54ce5fa
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 39 deletions.
10 changes: 4 additions & 6 deletions COPYRIGHT.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,10 @@ Comment: https://github.com/nlohmann/json
Copyright: 2013-2022, Niels Lohmann.
License: Expat

Files: ./*/_deps/ryu-src/
Comment: https://github.com/ulfjack/ryu
Copyright: Ulf Adams.
Stephan T. Lavavej.
Alexander Bolz.
License: Boost-1
Files: ./*/_deps/double-conversion-src/
Comment: https://github.com/google/double-conversion
Copyright: Copyright 2012 the V8 project authors.
License: BSD-3-clause

Files: ./*/_deps/sdl2-src/
Comment: https://github.com/libsdl-org/SDL
Expand Down
18 changes: 5 additions & 13 deletions cmake/External.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,12 @@ CPMAddPackage(
GIT_TAG json/v3.11.3
)

# ryu
# TODO: switch to Google/double-conversion
# double-conversion
CPMAddPackage(
NAME ryu
GITHUB_REPOSITORY ulfjack/ryu
GIT_TAG master
DOWNLOAD_ONLY ON
NAME double-conversion
GITHUB_REPOSITORY GameDevDeps/double-conversion
GIT_TAG double-conversion/v3.3.0
)
if(${ryu_ADDED})
# file(GLOB ryu_SOURCES ${ryu_SOURCE_DIR}/ryu/*.c)
add_library(ryu STATIC ${ryu_SOURCE_DIR}/ryu/d2fixed.c ${ryu_SOURCE_DIR}/ryu/d2s.c ${ryu_SOURCE_DIR}/ryu/f2s.c
${ryu_SOURCE_DIR}/ryu/s2d.c ${ryu_SOURCE_DIR}/ryu/s2f.c)
target_include_directories(ryu PUBLIC ${ryu_SOURCE_DIR})
endif()

lstg_group_deps_into_ide_folder(FOLDER "deps/Core"
TARGETS
Expand Down Expand Up @@ -154,7 +146,7 @@ lstg_group_deps_into_ide_folder(FOLDER "deps/Core"
bzip2.bz2
bzip2.bzip2
bzip2.bzip2recover
ryu
double-conversion.double-conversion
)

################################################################################
Expand Down
2 changes: 1 addition & 1 deletion src/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ set(LSTG_CORE_DEPS_PRIVATE
icu.libsicuuc
harfbuzz::harfbuzz-icu
lstg::IcuData
ryu
double-conversion::double-conversion
sdl2_sound::SDL2_sound-static)
if(LSTG_X11_ENABLE)
list(APPEND LSTG_CORE_DEPS_PRIVATE X11::xcb X11::X11_xcb)
Expand Down
24 changes: 16 additions & 8 deletions src/Core/Subsystem/Render/Font/HgeFontFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
*/
#include "HgeFontFactory.hpp"

extern "C" {
#include <ryu/ryu_parse.h>
}
#include <double-conversion/string-to-double.h>
#include <lstg/Core/Logging.hpp>
#include <lstg/Core/Encoding/Unicode.hpp>
#include <lstg/Core/Text/IniSaxParser.hpp>
Expand Down Expand Up @@ -227,12 +225,12 @@ namespace
auto ch = (i >= value.length() ? '\0' : value[i]);
if (ch == ',' || ch == '\0')
{
float v = 0;
string_view floatText = { value.data() + floatStart, i - floatStart };
auto err = ::s2f_n(floatText.data(), static_cast<int>(floatText.length()), &v);
if (err != SUCCESS)
int processedCount = 0;
float v = m_stStodConverter.StringToFloat(value.data() + floatStart, i - floatStart, &processedCount);
if (processedCount == 0)
{
LSTG_LOG_ERROR_CAT(HgeFontFactory, "Parse value \"{}\" fail", floatText);
LSTG_LOG_ERROR_CAT(HgeFontFactory, "Parse value \"{}\" fail",
string_view {value.data() + floatStart, i - floatStart});
return make_error_code(HgeFontLoadError::InvalidValue);
}
values[valueCnt++] = v;
Expand Down Expand Up @@ -262,6 +260,16 @@ namespace
IFontDependencyLoader* m_pDependencyLoader = nullptr;
int32_t m_iState = STATE_LOOKFOR_SECTION;
bool m_bBitmapRead = false;
double_conversion::StringToDoubleConverter m_stStodConverter {
double_conversion::StringToDoubleConverter::ALLOW_LEADING_SPACES |
double_conversion::StringToDoubleConverter::ALLOW_TRAILING_SPACES |
double_conversion::StringToDoubleConverter::ALLOW_SPACES_AFTER_SIGN |
double_conversion::StringToDoubleConverter::ALLOW_CASE_INSENSITIVITY,
0,
numeric_limits<double>::quiet_NaN(),
"infinity",
"nan"
};
};
}

Expand Down
44 changes: 33 additions & 11 deletions src/Core/Text/CmdlineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <lstg/Core/Text/CmdlineParser.hpp>

#include <cstring>
#include <ryu/ryu_parse.h>
#include <double-conversion/string-to-double.h>

using namespace std;
using namespace lstg;
Expand All @@ -18,23 +18,45 @@ Result<float> Text::detail::CmdlineArgumentConverter<float>::operator()(const Ar
if (argument.Type != ArgumentTypes::OptionWithValue)
return make_error_code(std::errc::invalid_argument);

float ret = 0.f;
auto ec = s2f_n(argument.Value.c_str(), static_cast<int>(argument.Value.size()), &ret);
if (ec == SUCCESS)
return ret;
return make_error_code(std::errc::invalid_argument);
double_conversion::StringToDoubleConverter converter {
double_conversion::StringToDoubleConverter::ALLOW_LEADING_SPACES |
double_conversion::StringToDoubleConverter::ALLOW_TRAILING_SPACES |
double_conversion::StringToDoubleConverter::ALLOW_SPACES_AFTER_SIGN |
double_conversion::StringToDoubleConverter::ALLOW_CASE_INSENSITIVITY,
0,
numeric_limits<double>::quiet_NaN(),
"infinity",
"nan"
};

int processedCount = 0;
float ret = converter.StringToFloat(argument.Value.c_str(), static_cast<int>(argument.Value.size()), &processedCount);
if (processedCount == 0)
return make_error_code(std::errc::invalid_argument);
return ret;
}

Result<double> Text::detail::CmdlineArgumentConverter<double>::operator()(const Argument& argument) noexcept
{
if (argument.Type != ArgumentTypes::OptionWithValue)
return make_error_code(std::errc::invalid_argument);

double ret = 0.;
auto ec = s2d_n(argument.Value.c_str(), static_cast<int>(argument.Value.size()), &ret);
if (ec == SUCCESS)
return ret;
return make_error_code(std::errc::invalid_argument);
double_conversion::StringToDoubleConverter converter {
double_conversion::StringToDoubleConverter::ALLOW_LEADING_SPACES |
double_conversion::StringToDoubleConverter::ALLOW_TRAILING_SPACES |
double_conversion::StringToDoubleConverter::ALLOW_SPACES_AFTER_SIGN |
double_conversion::StringToDoubleConverter::ALLOW_CASE_INSENSITIVITY,
0,
numeric_limits<double>::quiet_NaN(),
"infinity",
"nan"
};

int processedCount = 0;
double ret = converter.StringToDouble(argument.Value.c_str(), static_cast<int>(argument.Value.size()), &processedCount);
if (processedCount == 0)
return make_error_code(std::errc::invalid_argument);
return ret;
}

const Text::detail::Argument& CmdlineParser::operator[](size_t index) const noexcept
Expand Down

0 comments on commit 54ce5fa

Please sign in to comment.