ArkScript
A small, fast, functional and scripting language for video games
Utils.hpp
Go to the documentation of this file.
1/**
2 * @file Utils.hpp
3 * @author Alexandre Plateau ([email protected])
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
14#include <Ark/Platform.hpp>
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 Checks if a string is a valid double
49 *
50 * @param s the string
51 * @param output optional pointer to the output to avoid 2 conversions
52 * @return true on success
53 * @return false on failure
54 */
55 inline bool isDouble(const std::string& s, double* output = nullptr)
56 {
57 char* end = nullptr;
58 const double val = strtod(s.c_str(), &end);
59 if (output != nullptr)
60 *output = val;
61 return end != s.c_str() && *end == '\0' && val != HUGE_VAL;
62 }
63
64 /**
65 * @brief Calculate the Levenshtein distance between two strings
66 *
67 * @param str1
68 * @param str2
69 * @return std::size_t
70 */
71 ARK_API std::size_t levenshteinDistance(const std::string& str1, const std::string& str2);
72}
73
74#endif
#define ARK_API
Definition Module.hpp:28
ArkScript configuration macros.
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
bool isDouble(const std::string &s, double *output=nullptr)
Checks if a string is a valid double.
Definition Utils.hpp:55