Skip to content

Commit b350c01

Browse files
authored
Merge pull request #261 from biodranik/remove-const-ref
Replaced const std::string_view& with std::string_view for better performance
2 parents ac825b9 + d7da0ce commit b350c01

File tree

7 files changed

+50
-50
lines changed

7 files changed

+50
-50
lines changed

source/svgelement.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace lunasvg {
1212

13-
ElementID elementid(const std::string_view& name)
13+
ElementID elementid(std::string_view name)
1414
{
1515
static const struct {
1616
std::string_view name;
@@ -129,23 +129,23 @@ SVGElement::SVGElement(Document* document, ElementID id)
129129
{
130130
}
131131

132-
bool SVGElement::hasAttribute(const std::string_view& name) const
132+
bool SVGElement::hasAttribute(std::string_view name) const
133133
{
134134
auto id = propertyid(name);
135135
if(id == PropertyID::Unknown)
136136
return false;
137137
return hasAttribute(id);
138138
}
139139

140-
const std::string& SVGElement::getAttribute(const std::string_view& name) const
140+
const std::string& SVGElement::getAttribute(std::string_view name) const
141141
{
142142
auto id = propertyid(name);
143143
if(id == PropertyID::Unknown)
144144
return emptyString;
145145
return getAttribute(id);
146146
}
147147

148-
bool SVGElement::setAttribute(const std::string_view& name, const std::string& value)
148+
bool SVGElement::setAttribute(std::string_view name, const std::string& value)
149149
{
150150
auto id = propertyid(name);
151151
if(id == PropertyID::Unknown)
@@ -326,31 +326,31 @@ Rect SVGElement::paintBoundingBox() const
326326
return m_paintBoundingBox;
327327
}
328328

329-
SVGMarkerElement* SVGElement::getMarker(const std::string_view& id) const
329+
SVGMarkerElement* SVGElement::getMarker(std::string_view id) const
330330
{
331331
auto element = rootElement()->getElementById(id);
332332
if(element && element->id() == ElementID::Marker)
333333
return static_cast<SVGMarkerElement*>(element);
334334
return nullptr;
335335
}
336336

337-
SVGClipPathElement* SVGElement::getClipper(const std::string_view& id) const
337+
SVGClipPathElement* SVGElement::getClipper(std::string_view id) const
338338
{
339339
auto element = rootElement()->getElementById(id);
340340
if(element && element->id() == ElementID::ClipPath)
341341
return static_cast<SVGClipPathElement*>(element);
342342
return nullptr;
343343
}
344344

345-
SVGMaskElement* SVGElement::getMasker(const std::string_view& id) const
345+
SVGMaskElement* SVGElement::getMasker(std::string_view id) const
346346
{
347347
auto element = rootElement()->getElementById(id);
348348
if(element && element->id() == ElementID::Mask)
349349
return static_cast<SVGMaskElement*>(element);
350350
return nullptr;
351351
}
352352

353-
SVGPaintElement* SVGElement::getPainter(const std::string_view& id) const
353+
SVGPaintElement* SVGElement::getPainter(std::string_view id) const
354354
{
355355
auto element = rootElement()->getElementById(id);
356356
if(element && element->isPaintElement())
@@ -683,7 +683,7 @@ SVGRootElement* SVGRootElement::layoutIfNeeded()
683683
return this;
684684
}
685685

686-
SVGElement* SVGRootElement::getElementById(const std::string_view& id) const
686+
SVGElement* SVGRootElement::getElementById(std::string_view id) const
687687
{
688688
auto it = m_idCache.find(id);
689689
if(it == m_idCache.end())

source/svgelement.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ enum class ElementID : uint8_t {
108108
Use
109109
};
110110

111-
ElementID elementid(const std::string_view& name);
111+
ElementID elementid(std::string_view name);
112112

113113
using SVGNodeList = std::list<std::unique_ptr<SVGNode>>;
114114
using SVGPropertyList = std::forward_list<SVGProperty*>;
@@ -129,9 +129,9 @@ class SVGElement : public SVGNode {
129129
SVGElement(Document* document, ElementID id);
130130
virtual ~SVGElement() = default;
131131

132-
bool hasAttribute(const std::string_view& name) const;
133-
const std::string& getAttribute(const std::string_view& name) const;
134-
bool setAttribute(const std::string_view& name, const std::string& value);
132+
bool hasAttribute(std::string_view name) const;
133+
const std::string& getAttribute(std::string_view name) const;
134+
bool setAttribute(std::string_view name, const std::string& value);
135135

136136
const Attribute* findAttribute(PropertyID id) const;
137137
bool hasAttribute(PropertyID id) const;
@@ -159,10 +159,10 @@ class SVGElement : public SVGNode {
159159
virtual Rect strokeBoundingBox() const;
160160
virtual Rect paintBoundingBox() const;
161161

162-
SVGMarkerElement* getMarker(const std::string_view& id) const;
163-
SVGClipPathElement* getClipper(const std::string_view& id) const;
164-
SVGMaskElement* getMasker(const std::string_view& id) const;
165-
SVGPaintElement* getPainter(const std::string_view& id) const;
162+
SVGMarkerElement* getMarker(std::string_view id) const;
163+
SVGClipPathElement* getClipper(std::string_view id) const;
164+
SVGMaskElement* getMasker(std::string_view id) const;
165+
SVGPaintElement* getPainter(std::string_view id) const;
166166

167167
SVGElement* elementFromPoint(float x, float y);
168168

@@ -346,7 +346,7 @@ class SVGRootElement final : public SVGSVGElement {
346346

347347
SVGRootElement* layoutIfNeeded();
348348

349-
SVGElement* getElementById(const std::string_view& id) const;
349+
SVGElement* getElementById(std::string_view id) const;
350350
void addElementById(const std::string& id, SVGElement* element);
351351
void layout(SVGLayoutState& state) final;
352352

source/svglayoutstate.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,15 @@ static float parseNumberOrPercentage(std::string_view input, bool allowPercentag
110110
return value;
111111
}
112112

113-
static Length parseLength(const std::string_view& input, LengthNegativeMode mode, const Length& defaultValue)
113+
static Length parseLength(std::string_view input, LengthNegativeMode mode, const Length& defaultValue)
114114
{
115115
Length value;
116116
if(!value.parse(input, mode))
117117
value = defaultValue;
118118
return value;
119119
}
120120

121-
static BaselineShift parseBaselineShift(const std::string_view& input)
121+
static BaselineShift parseBaselineShift(std::string_view input)
122122
{
123123
if(input.compare("baseline") == 0)
124124
return BaselineShift::Type::Baseline;
@@ -167,7 +167,7 @@ static float parseFontSize(std::string_view input, const SVGLayoutState* state)
167167
}
168168

169169
template<typename Enum, unsigned int N>
170-
static Enum parseEnumValue(const std::string_view& input, const SVGEnumerationEntry<Enum>(&entries)[N], Enum defaultValue)
170+
static Enum parseEnumValue(std::string_view input, const SVGEnumerationEntry<Enum>(&entries)[N], Enum defaultValue)
171171
{
172172
for(const auto& entry : entries) {
173173
if(input == entry.second) {
@@ -178,7 +178,7 @@ static Enum parseEnumValue(const std::string_view& input, const SVGEnumerationEn
178178
return defaultValue;
179179
}
180180

181-
static Display parseDisplay(const std::string_view& input)
181+
static Display parseDisplay(std::string_view input)
182182
{
183183
static const SVGEnumerationEntry<Display> entries[] = {
184184
{Display::Inline, "inline"},
@@ -188,7 +188,7 @@ static Display parseDisplay(const std::string_view& input)
188188
return parseEnumValue(input, entries, Display::Inline);
189189
}
190190

191-
static Visibility parseVisibility(const std::string_view& input)
191+
static Visibility parseVisibility(std::string_view input)
192192
{
193193
static const SVGEnumerationEntry<Visibility> entries[] = {
194194
{Visibility::Visible, "visible"},
@@ -199,7 +199,7 @@ static Visibility parseVisibility(const std::string_view& input)
199199
return parseEnumValue(input, entries, Visibility::Visible);
200200
}
201201

202-
static Overflow parseOverflow(const std::string_view& input)
202+
static Overflow parseOverflow(std::string_view input)
203203
{
204204
static const SVGEnumerationEntry<Overflow> entries[] = {
205205
{Overflow::Visible, "visible"},
@@ -209,7 +209,7 @@ static Overflow parseOverflow(const std::string_view& input)
209209
return parseEnumValue(input, entries, Overflow::Visible);
210210
}
211211

212-
static PointerEvents parsePointerEvents(const std::string_view& input)
212+
static PointerEvents parsePointerEvents(std::string_view input)
213213
{
214214
static const SVGEnumerationEntry<PointerEvents> entries[] = {
215215
{PointerEvents::None,"none"},
@@ -228,7 +228,7 @@ static PointerEvents parsePointerEvents(const std::string_view& input)
228228
return parseEnumValue(input, entries, PointerEvents::Auto);
229229
}
230230

231-
static FontWeight parseFontWeight(const std::string_view& input)
231+
static FontWeight parseFontWeight(std::string_view input)
232232
{
233233
static const SVGEnumerationEntry<FontWeight> entries[] = {
234234
{FontWeight::Normal, "normal"},
@@ -249,7 +249,7 @@ static FontWeight parseFontWeight(const std::string_view& input)
249249
return parseEnumValue(input, entries, FontWeight::Normal);
250250
}
251251

252-
static FontStyle parseFontStyle(const std::string_view& input)
252+
static FontStyle parseFontStyle(std::string_view input)
253253
{
254254
static const SVGEnumerationEntry<FontStyle> entries[] = {
255255
{FontStyle::Normal, "normal"},
@@ -260,7 +260,7 @@ static FontStyle parseFontStyle(const std::string_view& input)
260260
return parseEnumValue(input, entries, FontStyle::Normal);
261261
}
262262

263-
static AlignmentBaseline parseAlignmentBaseline(const std::string_view& input)
263+
static AlignmentBaseline parseAlignmentBaseline(std::string_view input)
264264
{
265265
static const SVGEnumerationEntry<AlignmentBaseline> entries[] = {
266266
{AlignmentBaseline::Auto, "auto"},
@@ -280,7 +280,7 @@ static AlignmentBaseline parseAlignmentBaseline(const std::string_view& input)
280280
return parseEnumValue(input, entries, AlignmentBaseline::Auto);
281281
}
282282

283-
static DominantBaseline parseDominantBaseline(const std::string_view& input)
283+
static DominantBaseline parseDominantBaseline(std::string_view input)
284284
{
285285
static const SVGEnumerationEntry<DominantBaseline> entries[] = {
286286
{DominantBaseline::Auto, "auto"},
@@ -300,7 +300,7 @@ static DominantBaseline parseDominantBaseline(const std::string_view& input)
300300
return parseEnumValue(input, entries, DominantBaseline::Auto);
301301
}
302302

303-
static Direction parseDirection(const std::string_view& input)
303+
static Direction parseDirection(std::string_view input)
304304
{
305305
static const SVGEnumerationEntry<Direction> entries[] = {
306306
{Direction::Ltr, "ltr"},
@@ -310,7 +310,7 @@ static Direction parseDirection(const std::string_view& input)
310310
return parseEnumValue(input, entries, Direction::Ltr);
311311
}
312312

313-
static WritingMode parseWritingMode(const std::string_view& input)
313+
static WritingMode parseWritingMode(std::string_view input)
314314
{
315315
static const SVGEnumerationEntry<WritingMode> entries[] = {
316316
{WritingMode::Horizontal, "horizontal-tb"},
@@ -327,7 +327,7 @@ static WritingMode parseWritingMode(const std::string_view& input)
327327
return parseEnumValue(input, entries, WritingMode::Horizontal);
328328
}
329329

330-
static TextOrientation parseTextOrientation(const std::string_view& input)
330+
static TextOrientation parseTextOrientation(std::string_view input)
331331
{
332332
static const SVGEnumerationEntry<TextOrientation> entries[] = {
333333
{TextOrientation::Mixed, "mixed"},
@@ -337,7 +337,7 @@ static TextOrientation parseTextOrientation(const std::string_view& input)
337337
return parseEnumValue(input, entries, TextOrientation::Mixed);
338338
}
339339

340-
static TextAnchor parseTextAnchor(const std::string_view& input)
340+
static TextAnchor parseTextAnchor(std::string_view input)
341341
{
342342
static const SVGEnumerationEntry<TextAnchor> entries[] = {
343343
{TextAnchor::Start, "start"},
@@ -348,7 +348,7 @@ static TextAnchor parseTextAnchor(const std::string_view& input)
348348
return parseEnumValue(input, entries, TextAnchor::Start);
349349
}
350350

351-
static WhiteSpace parseWhiteSpace(const std::string_view& input)
351+
static WhiteSpace parseWhiteSpace(std::string_view input)
352352
{
353353
static const SVGEnumerationEntry<WhiteSpace> entries[] = {
354354
{WhiteSpace::Default, "default"},
@@ -363,7 +363,7 @@ static WhiteSpace parseWhiteSpace(const std::string_view& input)
363363
return parseEnumValue(input, entries, WhiteSpace::Default);
364364
}
365365

366-
static MaskType parseMaskType(const std::string_view& input)
366+
static MaskType parseMaskType(std::string_view input)
367367
{
368368
static const SVGEnumerationEntry<MaskType> entries[] = {
369369
{MaskType::Luminance, "luminance"},
@@ -373,7 +373,7 @@ static MaskType parseMaskType(const std::string_view& input)
373373
return parseEnumValue(input, entries, MaskType::Luminance);
374374
}
375375

376-
static FillRule parseFillRule(const std::string_view& input)
376+
static FillRule parseFillRule(std::string_view input)
377377
{
378378
static const SVGEnumerationEntry<FillRule> entries[] = {
379379
{FillRule::NonZero, "nonzero"},
@@ -383,7 +383,7 @@ static FillRule parseFillRule(const std::string_view& input)
383383
return parseEnumValue(input, entries, FillRule::NonZero);
384384
}
385385

386-
static LineCap parseLineCap(const std::string_view& input)
386+
static LineCap parseLineCap(std::string_view input)
387387
{
388388
static const SVGEnumerationEntry<LineCap> entries[] = {
389389
{LineCap::Butt, "butt"},
@@ -394,7 +394,7 @@ static LineCap parseLineCap(const std::string_view& input)
394394
return parseEnumValue(input, entries, LineCap::Butt);
395395
}
396396

397-
static LineJoin parseLineJoin(const std::string_view& input)
397+
static LineJoin parseLineJoin(std::string_view input)
398398
{
399399
static const SVGEnumerationEntry<LineJoin> entries[] = {
400400
{LineJoin::Miter, "miter"},

source/svgparser.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,17 @@ inline bool operator<(const RuleData& a, const RuleData& b) { return a.isLessTha
102102

103103
using RuleDataList = std::vector<RuleData>;
104104

105-
constexpr bool equals(const std::string_view& value, const std::string_view& subvalue)
105+
constexpr bool equals(std::string_view value, std::string_view subvalue)
106106
{
107107
return value.compare(subvalue) == 0;
108108
}
109109

110-
constexpr bool contains(const std::string_view& value, const std::string_view& subvalue)
110+
constexpr bool contains(std::string_view value, std::string_view subvalue)
111111
{
112112
return value.find(subvalue) != std::string_view::npos;
113113
}
114114

115-
constexpr bool includes(const std::string_view& value, const std::string_view& subvalue)
115+
constexpr bool includes(std::string_view value, std::string_view subvalue)
116116
{
117117
if(subvalue.empty() || subvalue.length() > value.length())
118118
return false;
@@ -130,21 +130,21 @@ constexpr bool includes(const std::string_view& value, const std::string_view& s
130130
return false;
131131
}
132132

133-
constexpr bool startswith(const std::string_view& value, const std::string_view& subvalue)
133+
constexpr bool startswith(std::string_view value, std::string_view subvalue)
134134
{
135135
if(subvalue.empty() || subvalue.length() > value.length())
136136
return false;
137137
return subvalue == value.substr(0, subvalue.size());
138138
}
139139

140-
constexpr bool endswith(const std::string_view& value, const std::string_view& subvalue)
140+
constexpr bool endswith(std::string_view value, std::string_view subvalue)
141141
{
142142
if(subvalue.empty() || subvalue.length() > value.length())
143143
return false;
144144
return subvalue == value.substr(value.size() - subvalue.size(), subvalue.size());
145145
}
146146

147-
constexpr bool dashequals(const std::string_view& value, const std::string_view& subvalue)
147+
constexpr bool dashequals(std::string_view value, std::string_view subvalue)
148148
{
149149
if(startswith(value, subvalue))
150150
return (value.length() == subvalue.length() || value.at(subvalue.length()) == '-');
@@ -717,7 +717,7 @@ bool Document::parse(const char* data, size_t length)
717717
std::string styleSheet;
718718
SVGElement* currentElement = nullptr;
719719
int ignoring = 0;
720-
auto handleText = [&](const std::string_view& text, bool in_cdata) {
720+
auto handleText = [&](std::string_view text, bool in_cdata) {
721721
if(text.empty() || currentElement == nullptr || ignoring > 0)
722722
return;
723723
if(currentElement->id() != ElementID::Text && currentElement->id() != ElementID::Tspan && currentElement->id() != ElementID::Style) {

source/svgparserutils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ constexpr bool skipDelimiter(std::string_view& input, char delimiter)
7878
return false;
7979
}
8080

81-
constexpr bool skipString(std::string_view& input, const std::string_view& value)
81+
constexpr bool skipString(std::string_view& input, std::string_view value)
8282
{
8383
if(input.size() >= value.size() && value == input.substr(0, value.size())) {
8484
input.remove_prefix(value.size());

source/svgproperty.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace lunasvg {
88

9-
PropertyID propertyid(const std::string_view& name)
9+
PropertyID propertyid(std::string_view name)
1010
{
1111
static const struct {
1212
std::string_view name;
@@ -67,7 +67,7 @@ PropertyID propertyid(const std::string_view& name)
6767
return it->value;
6868
}
6969

70-
PropertyID csspropertyid(const std::string_view& name)
70+
PropertyID csspropertyid(std::string_view name)
7171
{
7272
static const struct {
7373
std::string_view name;

source/svgproperty.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ enum class PropertyID : uint8_t {
9696
Y2
9797
};
9898

99-
PropertyID propertyid(const std::string_view& name);
100-
PropertyID csspropertyid(const std::string_view& name);
99+
PropertyID propertyid(std::string_view name);
100+
PropertyID csspropertyid(std::string_view name);
101101

102102
class SVGElement;
103103

0 commit comments

Comments
 (0)