Skip to content

Commit

Permalink
GUI: adopt C++20 std::string_view::starts_with
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiapoirier committed Feb 26, 2023
1 parent ce2236a commit ff3023f
Showing 1 changed file with 19 additions and 27 deletions.
46 changes: 19 additions & 27 deletions dfxgui/dfxguitextdisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ To contact the author, use the contact form at http:https://destroyfx.org
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <string_view>

Expand All @@ -53,23 +52,19 @@ static constexpr VSTGUI::CHoriTxtAlign DFXGUI_TextAlignmentToVSTGUI(dfx::TextAli
}

//-----------------------------------------------------------------------------
static bool DFXGUI_IsBitmapFont(char const* inFontName) noexcept
static bool DFXGUI_IsBitmapFont(std::string_view inFontName) noexcept
{
if (inFontName)
if (inFontName.compare(dfx::kFontName_Snooty10px) == 0)
{
std::string_view const fontNameView(inFontName);
if (fontNameView.compare(dfx::kFontName_Snooty10px) == 0)
{
return true;
}
if (fontNameView.compare(dfx::kFontName_Pasement9px) == 0)
{
return true;
}
if (fontNameView.compare(dfx::kFontName_Wetar16px) == 0)
{
return true;
}
return true;
}
if (inFontName.compare(dfx::kFontName_Pasement9px) == 0)
{
return true;
}
if (inFontName.compare(dfx::kFontName_Wetar16px) == 0)
{
return true;
}
return false;
}
Expand Down Expand Up @@ -299,7 +294,7 @@ void DGTextDisplay::refreshText()
//-----------------------------------------------------------------------------
bool DGTextDisplay::valueToTextProc_Generic(float inValue, char outTextUTF8[], void* /*inUserData*/)
{
return snprintf(outTextUTF8, DGTextDisplay::kTextMaxLength, "%.2f", inValue) > 0;
return std::snprintf(outTextUTF8, DGTextDisplay::kTextMaxLength, "%.2f", inValue) > 0;
}

//-----------------------------------------------------------------------------
Expand All @@ -308,7 +303,7 @@ bool DGTextDisplay::valueToTextProc_LinearToDb(float inValue, char outTextUTF8[]
constexpr auto units = "dB";
if (inValue <= 0.0f)
{
return snprintf(outTextUTF8, DGTextDisplay::kTextMaxLength, "-%s %s", dfx::kInfinityUTF8, units) > 0;
return std::snprintf(outTextUTF8, DGTextDisplay::kTextMaxLength, "-%s %s", dfx::kInfinityUTF8, units) > 0;
}

auto const decibelValue = dfx::math::Linear2dB(inValue);
Expand All @@ -317,14 +312,14 @@ bool DGTextDisplay::valueToTextProc_LinearToDb(float inValue, char outTextUTF8[]
auto const prefix = (decibelValue >= (0.01f / std::pow(10.f, precisionOffset))) ? "+" : "";
int precision = (std::fabs(decibelValue) >= 100.0f) ? 0 : ((std::fabs(decibelValue) >= 10.0f) ? 1 : 2);
precision = std::max(precision + static_cast<int>(precisionOffset), 0);
return snprintf(outTextUTF8, DGTextDisplay::kTextMaxLength, "%s%.*f %s", prefix, precision, decibelValue, units) > 0;
return std::snprintf(outTextUTF8, DGTextDisplay::kTextMaxLength, "%s%.*f %s", prefix, precision, decibelValue, units) > 0;
}

//-----------------------------------------------------------------------------
bool DGTextDisplay::valueToTextProc_Percent(float inValue, char outTextUTF8[], void* /*inUserData*/)
{
int const precision = ((inValue >= 0.1f) && (inValue <= 99.9f)) ? 1 : 0;
return snprintf(outTextUTF8, DGTextDisplay::kTextMaxLength, "%.*f%%", precision, inValue) > 0;
return std::snprintf(outTextUTF8, DGTextDisplay::kTextMaxLength, "%.*f%%", precision, inValue) > 0;
}

//-----------------------------------------------------------------------------
Expand All @@ -348,14 +343,11 @@ std::optional<float> DGTextDisplay::textToValueProc_DbToLinear(std::string const
{
return {};
}
if (inText.front() == '-')
if (inText.starts_with('-'))
{
auto const beginsWith = [&inText](auto const& matchText)
{
constexpr size_t offset = 1;
return (inText.length() >= (strlen(matchText) + offset)) && (inText.compare(offset, strlen(matchText), matchText) == 0);
};
if (beginsWith(dfx::kInfinityUTF8) || beginsWith("inf") || beginsWith("Inf") || beginsWith("INF"))
constexpr size_t signOffset = 1;
auto const unsignedText = std::string_view(inText).substr(signOffset);
if (unsignedText.starts_with(dfx::kInfinityUTF8) || unsignedText.starts_with("inf") || unsignedText.starts_with("Inf") || unsignedText.starts_with("INF"))
{
return 0.0f;
}
Expand Down

0 comments on commit ff3023f

Please sign in to comment.