ArkScript
A small, lisp-inspired, functional scripting language
Utils.hpp
Go to the documentation of this file.
1/**
2 * @file Utils.hpp
3 * @author Lex Plateau (lexplt.dev@gmail.com)
4 * @brief Lots of utilities about string, filesystem and more
5 * @date 2024-07-09
6 *
7 * @copyright Copyright (c) 2020-2025
8 *
9 */
10
11#ifndef INCLUDE_ARK_UTILS_HPP
12#define INCLUDE_ARK_UTILS_HPP
13
15
16#include <algorithm>
17#include <string>
18#include <vector>
19
20#include <cmath>
21
22namespace Ark::Utils
23{
24 /**
25 * @brief Cut a string into pieces, given a character separator
26 *
27 * @param source
28 * @param sep
29 * @return std::vector<std::string>
30 */
31 inline std::vector<std::string> splitString(const std::string& source, const char sep)
32 {
33 std::vector<std::string> output;
34 output.emplace_back();
35
36 for (const char c : source)
37 {
38 if (c != sep)
39 output.back() += c;
40 else
41 output.emplace_back(); // add empty string
42 }
43
44 return output;
45 }
46
47 /**
48 * @brief Remove spaces at the beginning of a string, in place
49 * @param s reference to a string
50 * @return std::string& the modified string
51 */
52 inline std::string& ltrim(std::string& s)
53 {
54 s.erase(
55 s.begin(),
56 std::ranges::find_if(s.begin(), s.end(), [](const unsigned char ch) {
57 return !std::isspace(ch);
58 }));
59 return s;
60 }
61
62 /**
63 * @brief Remove spaces at the end of a string, in place
64 * @param s reference to a string
65 * @return std::string& the modified string
66 */
67 inline std::string& rtrim(std::string& s)
68 {
69 s.erase(
70 std::ranges::find_if(s.rbegin(), s.rend(), [](const unsigned char ch) {
71 return !std::isspace(ch);
72 }).base(),
73 s.end());
74 return s;
75 }
76
77 /**
78 * @brief Checks if a string is a valid double
79 *
80 * @param s the string
81 * @param output optional pointer to the output to avoid 2 conversions
82 * @return true on success
83 * @return false on failure
84 */
85 inline bool isDouble(const std::string& s, double* output = nullptr)
86 {
87 char* end = nullptr;
88 const double val = strtod(s.c_str(), &end);
89 if (output != nullptr)
90 *output = val;
91 return end != s.c_str() && *end == '\0' && val != HUGE_VAL;
92 }
93
94 /**
95 * @brief Calculate the Levenshtein distance between two strings
96 *
97 * @param str1
98 * @param str2
99 * @return std::size_t
100 */
101 ARK_API std::size_t levenshteinDistance(const std::string& str1, const std::string& str2);
102}
103
104#endif
#define ARK_API
Definition Module.hpp:22
ArkScript configuration macros.
std::string & ltrim(std::string &s)
Remove spaces at the beginning of a string, in place.
Definition Utils.hpp:52
std::vector< std::string > splitString(const std::string &source, const char sep)
Cut a string into pieces, given a character separator.
Definition Utils.hpp:31
ARK_API std::size_t levenshteinDistance(const std::string &str1, const std::string &str2)
Calculate the Levenshtein distance between two strings.
Definition Utils.cpp:5
std::string & rtrim(std::string &s)
Remove spaces at the end of a string, in place.
Definition Utils.hpp:67
bool isDouble(const std::string &s, double *output=nullptr)
Checks if a string is a valid double.
Definition Utils.hpp:85