Skip to content

Commit b09e813

Browse files
glebmxzyfer
authored andcommitted
Refactor: Add starts/ends_with functions
Adds `starts_with` and `ends_with` string functions to `util.hpp`. This will hopefully prevent regressions caused by incorrect ad-hoc implementations, such as the one fixed by ef56d81
1 parent 280ffd8 commit b09e813

File tree

6 files changed

+37
-15
lines changed

6 files changed

+37
-15
lines changed

src/file.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "utf8_string.hpp"
2323
#include "sass_functions.hpp"
2424
#include "error_handling.hpp"
25+
#include "util.hpp"
2526
#include "sass2scss.h"
2627

2728
#ifdef _WIN32

src/fn_colors.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,17 @@
33
#include "ast.hpp"
44
#include "fn_utils.hpp"
55
#include "fn_colors.hpp"
6+
#include "util.hpp"
67

78
namespace Sass {
89

910
namespace Functions {
1011

1112
bool special_number(String_Constant_Ptr s) {
12-
if (s) {
13-
static const char* const calc = "calc(";
14-
static const char* const var = "var(";
15-
const std::string& str = s->value();
16-
return str.compare(0, strlen(calc), calc) == 0 ||
17-
str.compare(0, strlen(var), var) == 0;
18-
}
19-
return false;
13+
if (s == nullptr) return false;
14+
const std::string& str = s->value();
15+
return starts_with(str, "calc(") ||
16+
starts_with(str, "var(");
2017
}
2118

2219
Signature rgb_sig = "rgb($red, $green, $blue)";

src/output.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "sass.hpp"
22
#include "ast.hpp"
33
#include "output.hpp"
4+
#include "util.hpp"
45

56
namespace Sass {
67

src/output.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@
1111
namespace Sass {
1212
class Context;
1313

14-
// Refactor to make it generic to find linefeed (look behind)
15-
inline bool ends_with(std::string const & value, std::string const & ending)
16-
{
17-
if (ending.size() > value.size()) return false;
18-
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
19-
}
20-
2114
class Output : public Inspect {
2215
protected:
2316
using Inspect::operator();

src/plugins.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <iostream>
33
#include "output.hpp"
44
#include "plugins.hpp"
5+
#include "util.hpp"
56

67
#ifdef _WIN32
78
#include <windows.h>

src/util.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef SASS_UTIL_H
22
#define SASS_UTIL_H
33

4+
#include <cstring>
45
#include <vector>
56
#include <string>
67
#include <assert.h>
@@ -34,6 +35,34 @@ namespace Sass {
3435

3536
bool peek_linefeed(const char* start);
3637

38+
// C++20 `starts_with` equivalent.
39+
// See https://en.cppreference.com/w/cpp/string/basic_string/starts_with
40+
inline bool starts_with(const std::string& str, const char* prefix, size_t prefix_len) {
41+
return str.compare(0, prefix_len, prefix) == 0;
42+
}
43+
44+
inline bool starts_with(const std::string& str, const char* prefix) {
45+
return starts_with(str, prefix, std::strlen(prefix));
46+
}
47+
48+
// C++20 `ends_with` equivalent.
49+
// See https://en.cppreference.com/w/cpp/string/basic_string/ends_with
50+
inline bool ends_with(const std::string& str, const std::string& suffix) {
51+
return suffix.size() <= str.size() && std::equal(suffix.rbegin(), suffix.rend(), str.rbegin());
52+
}
53+
54+
inline bool ends_with(const std::string& str, const char* suffix, size_t suffix_len) {
55+
if (suffix_len > str.size()) return false;
56+
const char* suffix_it = suffix + suffix_len;
57+
const char* str_it = str.c_str() + str.size();
58+
while (suffix_it != suffix) if (*(--suffix_it) != *(--str_it)) return false;
59+
return true;
60+
}
61+
62+
inline bool ends_with(const std::string& str, const char* suffix) {
63+
return ends_with(str, suffix, std::strlen(suffix));
64+
}
65+
3766
namespace Util {
3867

3968
std::string rtrim(const std::string& str);

0 commit comments

Comments
 (0)