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