-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathString.extern.h
150 lines (129 loc) · 4.87 KB
/
String.extern.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2022, EA31337 Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+
/*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Prevents processing this includes file for the second time.
#ifndef __MQL__
#pragma once
// Includes.
#include <stdarg.h>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <sstream>
#include <tuple>
#include "Math.extern.h"
#include "Std.h"
#include "Terminal.define.h"
// Define external global functions.
double StringToDouble(string value) { return std::stod(value); }
auto StringFind(const string string_value, string match_substring, int start_pos = 0) -> int {
return string_value.find(match_substring);
}
int StringLen(string string_value) { return string_value.size(); }
int StringSplit(const string& string_value, const unsigned short separator, ARRAY_REF(string, result)) {
auto start = 0U;
auto end = string_value.find((char)separator);
while (end != std::string::npos) {
result.str().push_back(string_value.substr(start, end - start));
start = end + 1; // 1 - size of the separator.
end = string_value.find((char)separator, start);
}
return result.size();
}
long StringToInteger(string value) { return std::stol(value); }
string IntegerToString(long number, int str_len = 0, unsigned short fill_symbol = ' ') {
return std::to_string(number);
}
template <class... Args>
std::string StringFormat(std::string f, Args&&... args) {
int size = snprintf(nullptr, 0, f.c_str(), args...);
std::string res;
res.resize(size);
snprintf(&res[0], size + 1, f.c_str(), args...);
return res;
}
template <typename Arg, typename... Args>
void PrintTo(std::ostream& out, Arg&& arg, Args&&... args) {
out << std::forward<Arg>(arg);
using expander = int[];
(void)expander{0, (void(out << std::forward<Args>(args)), 0)...};
out << "\n";
out.flush();
}
template <typename Arg, typename... Args>
void Print(Arg&& arg, Args&&... args) {
PrintTo(std::cout, arg, args...);
}
template <typename Arg, typename... Args>
void Alert(Arg&& arg, Args&&... args) {
PrintTo(std::cerr, arg, args...);
}
template <class... Args>
void PrintFormat(const std::string& fmt, Args&&... args) {
std::cout << StringFormat(fmt, args...) << std::endl;
}
string StringSubstr(string string_value, int start_pos, int length = -1) {
return string_value.substr(start_pos, length == -1 ? (string_value.size() - start_pos) : length);
}
unsigned short StringGetCharacter(string string_value, int pos) {
if (pos < 0 || pos >= string_value.size()) {
Alert("Character index out of string boundary! Position passed: ", pos, ", string passed: \"", string_value, "\"");
}
return string_value[pos];
}
int StringToCharArray(string text_string, ARRAY_REF(unsigned char, array), int start = 0, int count = -1,
unsigned int codepage = CP_ACP) {
if (count == -1) count = text_string.size();
for (int i = start; i < MathMin(start + count, (int)text_string.size()); ++i)
array.push((unsigned char)text_string[i]);
return array.size();
}
bool StringInit(string& string_var, int new_len = 0, unsigned short character = 0) {
string_var = string(new_len, (char)character);
return true;
}
/**
* It replaces all the found substrings of a string by a set sequence of symbols.
*
* @docs
* - https://www.mql5.com/en/docs/strings/stringreplace
*/
int StringReplace(string& str, const string& find, const string& replacement) {
int num_replacements = 0;
for (size_t pos = 0;; pos += replacement.length()) {
// Locate the substring to replace
pos = str.find(find, pos);
if (pos == string::npos) break;
// Replace by erasing and inserting
str.erase(pos, find.length());
str.insert(pos, replacement);
++num_replacements;
}
return num_replacements;
}
string StringToLower(string str) {
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) { return ::tolower(c); });
return str;
}
string StringToUpper(string str) {
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) { return ::toupper(c); });
return str;
}
#endif