Skip to content

Commit 8e6fdab

Browse files
committed
review.
1 parent 4cd6a4b commit 8e6fdab

File tree

8 files changed

+34
-18
lines changed

8 files changed

+34
-18
lines changed

src/base/anim/easing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Easing::Easing(const std::string &name)
4545
func = EasingGradient::Bezier(Geom::Point{0.42, 0},
4646
Geom::Point{0.58, 1});
4747
}
48-
else if (const Text::FuncString f(nameCopy, false);
48+
else if (const Text::FuncString f(nameCopy);
4949
f.getName() == "cubic-bezier") {
5050
if (f.getParams().size() != 4)
5151
throw std::logic_error("parameter count missmatch");

src/base/gfx/color.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Color Color::fromString(const std::string &string)
4444
((g << 4U) + g) / 255.0,
4545
((b << 4U) + b) / 255.0};
4646
}
47-
if (const Text::FuncString f(string, false); !f.isEmpty()) {
47+
if (const Text::FuncString f(string); !f.isEmpty()) {
4848
using Conv::parse;
4949

5050
if (f.getName() == "rgb") {

src/base/gfx/colortransform.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <utility>
77

88
#include "base/text/funcstring.h"
9+
#include "base/text/smartstring.h"
910

1011
#include "color.h"
1112

@@ -14,10 +15,9 @@ namespace Gfx
1415

1516
ColorTransform ColorTransform::fromString(const std::string &code)
1617
{
17-
const Text::FuncString func(code, false);
18+
if (Text::SmartString::trim_view(code) == "none") return None();
1819

19-
if (func.getName() == "none" && func.getParams().empty())
20-
return None();
20+
const Text::FuncString func(code);
2121

2222
if (func.isEmpty()) return {};
2323

src/base/text/funcstring.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ FuncString::FuncString(std::string code, bool throwOnError)
2323
}
2424

2525
if (parts[1].empty() || parts[1].back() != ')') {
26-
if (!throwOnError) {
27-
name = std::move(code);
28-
return;
29-
}
26+
if (!throwOnError) return;
3027

3128
throw std::logic_error("invalid function format");
3229
}

src/base/text/funcstring.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ class FuncString
1212
public:
1313
using Params = std::vector<std::string>;
1414

15-
explicit FuncString(std::string code, bool throwOnError = true);
15+
[[nodiscard]] static FuncString fromString(
16+
const std::string &code)
17+
{
18+
return FuncString{code, true};
19+
}
20+
21+
explicit FuncString(std::string code, bool throwOnError = false);
22+
1623
[[nodiscard]] bool isEmpty() const { return name.empty(); }
1724
[[nodiscard]] const std::string &getName() const { return name; }
1825
[[nodiscard]] const Params &getParams() const { return params; }

src/base/text/smartstring.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
#include <algorithm>
55
#include <cctype>
66
#include <cstddef>
7+
#include <iterator>
78
#include <string>
9+
#include <string_view>
810
#include <utility>
911
#include <vector>
1012

@@ -15,18 +17,25 @@
1517

1618
namespace Text::SmartString
1719
{
18-
void trim(std::string &string)
20+
21+
std::string_view trim_view(const std::string_view &string)
1922
{
2023
auto ignore = [](unsigned char c)
2124
{
2225
return std::isspace(c);
2326
};
24-
string.erase(string.begin(),
25-
std::find_if_not(string.begin(), string.end(), ignore));
26-
string.erase(
27-
std::find_if_not(string.rbegin(), string.rend(), ignore)
28-
.base(),
29-
string.end());
27+
const auto *start = std::ranges::find_if_not(string, ignore);
28+
return {start,
29+
std::find_if_not(string.rbegin(),
30+
std::reverse_iterator{start},
31+
ignore)
32+
.base()};
33+
}
34+
35+
void trim(std::string &string)
36+
{
37+
auto &&view = trim_view(string);
38+
string.assign(view.begin(), view.end());
3039
}
3140

3241
std::vector<std::string> split(const std::string &str,

src/base/text/smartstring.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ namespace SmartString
1919
bool ignoreEmpty = false,
2020
const char *parens = nullptr);
2121

22+
std::string_view trim_view(const std::string_view &string);
23+
2224
void trim(std::string &string);
2325

2426
[[nodiscard]] std::string fromPhysicalValue(double value,

test/unit/base/text/funcstring.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "base/text/funcstring.h"
22

33
#include "../../util/test.h"
4+
#include "base/conv/parse.h"
45

56
using test::check;
67
using test::collection;
@@ -101,7 +102,7 @@ const static auto tests =
101102
{
102103
throws<std::exception>() << []
103104
{
104-
FuncString("foo");
105+
Conv::parse<FuncString>("foo");
105106
};
106107
})
107108

0 commit comments

Comments
 (0)