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 * @version 1.0
6 * @date 2024-07-09
7 *
8 * @copyright Copyright (c) 2020-2024
9 *
10 */
11
12#ifndef INCLUDE_ARK_UTILS_HPP
13#define INCLUDE_ARK_UTILS_HPP
14
15#include <Ark/Platform.hpp>
16
17#include <algorithm>
18#include <string>
19#include <vector>
20
21#include <cmath>
22
23namespace Ark::Utils
24{
25 /**
26 * @brief Cut a string into pieces, given a character separator
27 *
28 * @param source
29 * @param sep
30 * @return std::vector<std::string>
31 */
32 inline std::vector<std::string> splitString(const std::string& source, const char sep)
33 {
34 std::vector<std::string> output;
35 output.emplace_back();
36
37 for (const char c : source)
38 {
39 if (c != sep)
40 output.back() += c;
41 else
42 output.emplace_back(); // add empty string
43 }
44
45 return output;
46 }
47
48 /**
49 * @brief Checks if a string is a valid double
50 *
51 * @param s the string
52 * @param output optional pointer to the output to avoid 2 conversions
53 * @return true on success
54 * @return false on failure
55 */
56 inline bool isDouble(const std::string& s, double* output = nullptr)
57 {
58 char* end = nullptr;
59 const double val = strtod(s.c_str(), &end);
60 if (output != nullptr)
61 *output = val;
62 return end != s.c_str() && *end == '\0' && val != HUGE_VAL;
63 }
64
65 /**
66 * @brief Calculate the Levenshtein distance between two strings
67 *
68 * @param str1
69 * @param str2
70 * @return std::size_t
71 */
72 ARK_API std::size_t levenshteinDistance(const std::string& str1, const std::string& str2);
73}
74
75#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:32
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:56