Fixed most errors, moved things around

This commit is contained in:
sam-astro 2022-01-05 15:51:44 -05:00
parent b1a1133d85
commit 79b1b7102d
8 changed files with 216 additions and 191 deletions

View File

@ -22,76 +22,6 @@ using namespace boost;
unordered_map<string, any> globalVariableValues; unordered_map<string, any> globalVariableValues;
unordered_map<string, vector<vector<string>>> functionValues; unordered_map<string, vector<vector<string>>> functionValues;
bool isNumber(const string& str)
{
for (char const& c : str) {
if (isdigit(c) == 0 && c != '.') return false;
}
return true;
}
bool stob(string str) {
transform(str.begin(), str.end(), str.begin(), ::tolower);
istringstream is(str);
bool b;
is >> boolalpha >> b;
return b;
}
string StringRaw(const string& s)
{
string str = trim(s);
if (str.size() < 3)
return str;
string withoutQuotes;
if (str[0] != '\"')
withoutQuotes += str[0];
withoutQuotes += str.substr(1, str.size()-2);
if (str[str.size() - 1] != '\"')
withoutQuotes += str[str.size() - 1];
return withoutQuotes;
}
string Quoted(const string& s)
{
string str = trim(s);
string withQuotes;
if (str[0] != '\"')
withQuotes += '\"';
withQuotes += str;
if (str[str.size() - 1] != '\"')
withQuotes += '\"';
return withQuotes;
}
string RMParenthesis(const string& s)
{
string str = trim(s);
string withoutParenthesis;
if (str[0] != '(')
withoutParenthesis += str[0];
withoutParenthesis += str.substr(1, str.size()-2);
if (str[str.size() - 1] != ')')
withoutParenthesis += str[str.size() - 1];
return withoutParenthesis;
}
any GetVariableValue(const string& varName, const unordered_map<string, any>& variableVals) any GetVariableValue(const string& varName, const unordered_map<string, any>& variableVals)
{ {
auto iA = variableVals.find(varName); auto iA = variableVals.find(varName);
@ -155,19 +85,6 @@ bool IsFunction(const string& funcName)
return false; return false;
} }
int LogWarning(const string& warningText)
{
cerr << "\x1B[33mWARNING: " << warningText << "\033[0m\t\t" << endl;
return 1;
}
int CriticalError(const string& errorText)
{
cerr << "\x1B[31mERROR: " << errorText << "\033[0m\t\t" << endl;
exit(EXIT_FAILURE);
return 2;
}
any EvalExpression(const string& ex, const unordered_map<string, any>& variableVals) any EvalExpression(const string& ex, const unordered_map<string, any>& variableVals)
{ {
string expression = trim(ex); string expression = trim(ex);
@ -311,16 +228,18 @@ bool BooleanLogic(const string& valA, const string& determinant, const string& v
if (determinant == "==") if (determinant == "==")
return AnyAsString(valARealValue) == AnyAsString(valBRealValue); return AnyAsString(valARealValue) == AnyAsString(valBRealValue);
if (determinant == "!=") else if (determinant == "!=")
return AnyAsString(valARealValue) != AnyAsString(valBRealValue); return AnyAsString(valARealValue) != AnyAsString(valBRealValue);
if (determinant == ">=") else if (determinant == ">=")
return AnyAsFloat(valARealValue) >= AnyAsFloat(valBRealValue); return AnyAsFloat(valARealValue) >= AnyAsFloat(valBRealValue);
if (determinant == "<=") else if (determinant == "<=")
return AnyAsFloat(valARealValue) <= AnyAsFloat(valBRealValue); return AnyAsFloat(valARealValue) <= AnyAsFloat(valBRealValue);
if (determinant == ">") else if (determinant == ">")
return AnyAsFloat(valARealValue) > AnyAsFloat(valBRealValue); return AnyAsFloat(valARealValue) > AnyAsFloat(valBRealValue);
if (determinant == "<") else if (determinant == "<")
return AnyAsFloat(valARealValue) < AnyAsFloat(valBRealValue); return AnyAsFloat(valARealValue) < AnyAsFloat(valBRealValue);
else
LogWarning("unrecognized determinant \'" + determinant + "\'");
return false; return false;
} }
@ -339,7 +258,8 @@ int varOperation(const vector<string>& str, unordered_map<string, any>& variable
variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) * AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues)); variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) * AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues));
else if (str[1] == "/=") else if (str[1] == "/=")
variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) / AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues)); variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) / AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues));
else
LogWarning("unrecognized operator \'" + str[1] + "\'");
//cout << variables[v] << " is " << variableValues[v] << endl; //cout << variables[v] << " is " << variableValues[v] << endl;
return 0; return 0;
} }
@ -355,24 +275,25 @@ int varOperation(const vector<string>& str, unordered_map<string, any>& variable
globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) * AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues)); globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) * AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues));
else if (str[1] == "/=") else if (str[1] == "/=")
globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) / AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues)); globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) / AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues));
else
LogWarning("unrecognized operator \'" + str[1] + "\'");
//cout << variables[v] << " is " << variableValues[v] << endl; //cout << variables[v] << " is " << variableValues[v] << endl;
return 0; return 0;
} }
LogWarning("uninitialized variable or typo in \'" << str[0] << "\'"); LogWarning("uninitialized variable or typo in \'" + str[0] + "\'");
return 1; return 1;
} }
any ProcessLine(const vector<vector<string>>& words, const int lineNum, unordered_map<string, any>& variableValues) any ProcessLine(const vector<vector<string>>& words, int lineNum, unordered_map<string, any>& variableValues)
{ {
if (words[lineNum][0][0] == '/' && words[lineNum][0][1] == '/') if (words[lineNum][0][0] == '/' && words[lineNum][0][1] == '/')
return; return any{};
// If print statement (deprecated, now use CPP.System.Print() function) // If print statement (deprecated, now use CPP.System.Print() function)
else if (words[lineNum][0] == "print") else if (words[lineNum][0] == "print")
{ {
cout << AnyAsString(EvalExpression(unWrapVec(vector<string>(words[lineNum].begin() + 1, words[lineNum].end())), variableValues)) << endl; cout << AnyAsString(EvalExpression(unWrapVec(vector<string>(words[lineNum].begin() + 1, words[lineNum].end())), variableValues)) << endl;
return; return any{};
} }
// Check if function return // Check if function return
@ -387,7 +308,7 @@ any ProcessLine(const vector<vector<string>>& words, const int lineNum, unordere
else if (IsFunction(trim(split(words[lineNum][0], '(')[0]))) else if (IsFunction(trim(split(words[lineNum][0], '(')[0])))
{ {
ExecuteFunction(trim(split(words[lineNum][0], '(')[0]), VarValues(split(RMParenthesis(replace(unWrapVec(words[lineNum]), trim(split(words[lineNum][0], '(')[0]), "")), ','), variableValues)); ExecuteFunction(trim(split(words[lineNum][0], '(')[0]), VarValues(split(RMParenthesis(replace(unWrapVec(words[lineNum]), trim(split(words[lineNum][0], '(')[0]), "")), ','), variableValues));
return; return any{};
} }
// Iterate through all types to see if line inits or // Iterate through all types to see if line inits or
@ -395,7 +316,7 @@ any ProcessLine(const vector<vector<string>>& words, const int lineNum, unordere
else if (countInVector(types, words[lineNum][0]) > 0) else if (countInVector(types, words[lineNum][0]) > 0)
{ {
variableValues[words[lineNum][1]] = EvalExpression(unWrapVec(vector<string>(words[lineNum].begin() + 3, words[lineNum].end())), variableValues); variableValues[words[lineNum][1]] = EvalExpression(unWrapVec(vector<string>(words[lineNum].begin() + 3, words[lineNum].end())), variableValues);
return; return any{};
} }
// Check existing variables: If matches, then it means // Check existing variables: If matches, then it means
@ -404,7 +325,7 @@ any ProcessLine(const vector<vector<string>>& words, const int lineNum, unordere
{ {
// Evaluates what the operator (ex. '=', '+=') does to the value on the left by the value on the right // Evaluates what the operator (ex. '=', '+=') does to the value on the left by the value on the right
varOperation(vector<string>(words[lineNum].begin(), words[lineNum].end()), variableValues); varOperation(vector<string>(words[lineNum].begin(), words[lineNum].end()), variableValues);
return; return any{};
} }
// Gathers while loop contents // Gathers while loop contents
@ -444,7 +365,7 @@ any ProcessLine(const vector<vector<string>>& words, const int lineNum, unordere
return returnVal; return returnVal;
} }
} }
return; return any{};
} }
// Gathers if statement contents // Gathers if statement contents
@ -481,8 +402,8 @@ any ProcessLine(const vector<vector<string>>& words, const int lineNum, unordere
//Iterate through all lines in if statement //Iterate through all lines in if statement
for (int l = 0; l < (int)ifContents.size(); l++) for (int l = 0; l < (int)ifContents.size(); l++)
{ {
string returnVal = ProcessLine(innerWords, l, variableValues); any returnVal = ProcessLine(innerWords, l, variableValues);
if (returnVal != 0) if (!returnVal.empty())
return returnVal; return returnVal;
} }
} }
@ -510,16 +431,16 @@ any ProcessLine(const vector<vector<string>>& words, const int lineNum, unordere
vector<vector<string>> innerWords; vector<vector<string>> innerWords;
for (int i = 0; i < (int)elseContents.size(); i++) for (int i = 0; i < (int)elseContents.size(); i++)
words.push_back(split(elseContents[i], ' ')); innerWords.push_back(split(elseContents[i], ' '));
//Iterate through all lines in else statement //Iterate through all lines in else statement
for (int lineNum = 0; lineNum < (int)elseContents.size(); lineNum++) for (int lineNum = 0; lineNum < (int)elseContents.size(); lineNum++)
{ {
ProcessLine(innerWords, lineNum, variableValues); ProcessLine(innerWords, lineNum, variableValues);
} }
return; return any{};
} }
return; return any{};
} }
//// Gathers else statement contents //// Gathers else statement contents
//if (words[lineNum][0] == "else") //if (words[lineNum][0] == "else")
@ -527,7 +448,7 @@ any ProcessLine(const vector<vector<string>>& words, const int lineNum, unordere
// //
//} //}
return; return any{};
} }
any ExecuteFunction(const string functionName, const vector<any> inputVarVals) any ExecuteFunction(const string functionName, const vector<any> inputVarVals)
@ -536,10 +457,10 @@ any ExecuteFunction(const string functionName, const vector<any> inputVarVals)
vector<vector<string>> words = functionValues[functionName]; vector<vector<string>> words = functionValues[functionName];
unordered_map<string, any> variableValues; unordered_map<string, any> variableValues;
vector<string> args = split(replace(functionValues[functionName][0][0], ','); vector<string> args = split(functionValues[functionName][0][0], ',');
for (int i = 0; i < (int)inputVarVals.size(); i++) for (int i = 0; i < (int)inputVarVals.size(); i++)
{ {
variableValues[trim(args[i])] = EvalExpression(inputVarVals[i], variables, variableValues); variableValues[trim(args[i])] = EvalExpression(AnyAsString(inputVarVals[i]), variableValues);
} }
//Iterate through all lines in function //Iterate through all lines in function
@ -548,16 +469,16 @@ any ExecuteFunction(const string functionName, const vector<any> inputVarVals)
any returnVal = 0; any returnVal = 0;
try try
{ {
returnVal = ProcessLine(words, lineNum, variables, variableValues); returnVal = ProcessLine(words, lineNum, variableValues);
} }
catch (const std::exception&) catch (const std::exception&)
{ {
CriticalError("\'" << unWrapVec(words[lineNum]) << "\'\nIn function: " << functionName << "\nLine: " << lineNum); LogCriticalError("\'" + unWrapVec(words[lineNum]) + "\'\nIn function: " + functionName + "\nLine: " + to_string(lineNum));
} }
if (!returnVal.empty()) if (!returnVal.empty())
return returnVal; return returnVal;
} }
return; return any{};
} }
int parseSlang(string script) int parseSlang(string script)
@ -578,7 +499,7 @@ int parseSlang(string script)
{ {
vector<vector<string>> functionContents; vector<vector<string>> functionContents;
string functName = split(words[lineNum][1], "(")[0]; string functName = split(words[lineNum][1], '(')[0];
string args = ""; string args = "";
for (int w = 1; w < (int)words[lineNum].size(); w++) { for (int w = 1; w < (int)words[lineNum].size(); w++) {
@ -616,7 +537,8 @@ int parseSlang(string script)
globalVariableValues[words[lineNum][1]] = stof(words[lineNum][3]); globalVariableValues[words[lineNum][1]] = stof(words[lineNum][3]);
else if (words[lineNum][0] == "bool") else if (words[lineNum][0] == "bool")
globalVariableValues[words[lineNum][1]] = stob(words[lineNum][3]); globalVariableValues[words[lineNum][1]] = stob(words[lineNum][3]);
LogWarning("unrecognized type \'" + words[lineNum][0] + "\' on line: " + lineNum); else
LogWarning("unrecognized type \'" + words[lineNum][0] + "\' on line: " + to_string(lineNum));
} }
} }

View File

@ -2,7 +2,11 @@
#ifndef ANYOPS_H #ifndef ANYOPS_H
#define ANYOPS_H #define ANYOPS_H
#include "builtin.h"
#include <any>
using namespace boost; using namespace boost;
using namespace std;
// Will convert type 'any' val to a bool // Will convert type 'any' val to a bool
bool AnyAsBool(const any& val) bool AnyAsBool(const any& val)
@ -31,7 +35,7 @@ bool AnyAsBool(const any& val)
} }
catch (boost::bad_any_cast &e) // Does not convert, return catch (boost::bad_any_cast &e) // Does not convert, return
{ {
LogWarning("invalid conversion to type \'bool\'") LogWarning("invalid conversion to type \'bool\'");
return false; return false;
} }
} }
@ -68,7 +72,7 @@ string AnyAsString(const any& val)
} }
catch (boost::bad_any_cast &e) // Does not convert, return catch (boost::bad_any_cast &e) // Does not convert, return
{ {
LogWarning("invalid conversion to type \'string\'") LogWarning("invalid conversion to type \'string\'");
return ""; return "";
} }
} }
@ -105,7 +109,7 @@ float AnyAsFloat(const any& val)
} }
catch (boost::bad_any_cast &e) // Does not convert, return catch (boost::bad_any_cast &e) // Does not convert, return
{ {
LogWarning("invalid conversion to type \'float\'") LogWarning("invalid conversion to type \'float\'");
return 0; return 0;
} }
} }
@ -142,7 +146,7 @@ int AnyAsInt(const any& val)
} }
catch (boost::bad_any_cast &e) // Does not convert, return catch (boost::bad_any_cast &e) // Does not convert, return
{ {
LogWarning("invalid conversion to type \'int\'") LogWarning("invalid conversion to type \'int\'");
return 0; return 0;
} }
} }
@ -182,7 +186,7 @@ int any_type(const any& val)
} }
catch (boost::bad_any_cast &e) // Does not convert, return catch (boost::bad_any_cast &e) // Does not convert, return
{ {
LogWarning("variable has no type") LogWarning("variable has no type");
return -1; return -1;
} }
} }

View File

@ -8,6 +8,7 @@
#include <limits> #include <limits>
#include <algorithm> #include <algorithm>
#include <boost/any.hpp> #include <boost/any.hpp>
#include <unordered_map>
#include "strops.h" #include "strops.h"
#include "graphics.h" #include "graphics.h"
#include "anyops.h" #include "anyops.h"
@ -16,15 +17,29 @@ using namespace std;
vector<string> types = { "int", "float", "string", "bool", "void", "null" }; vector<string> types = { "int", "float", "string", "bool", "void", "null" };
unordered_map<string, vector<string>> builtinFunctionValues; unordered_map<string, vector<vector<string>>> builtinFunctionValues;
unordered_map<string, any>& builtinVarVals; unordered_map<string, any> builtinVarVals;
Parser mainWindow; Parser mainWindow;
int LogWarning(const string& warningText)
{
cerr << "\x1B[33mWARNING: " << warningText << "\033[0m\t\t" << endl;
return 1;
}
int LogCriticalError(const string& errorText)
{
cerr << "\x1B[31mERROR: " << errorText << "\033[0m\t\t" << endl;
exit(EXIT_FAILURE);
return 2;
}
// Initial script processing, which loads variables and functions from builtin // Initial script processing, which loads variables and functions from builtin
int GetBuiltins(const string& s) int GetBuiltins(const string& s)
{ {
script = replace(s, " ", "\t"); string script = replace(s, " ", "\t");
vector<string> lines = split(script, '\n'); vector<string> lines = split(script, '\n');
vector<vector<string>> words; vector<vector<string>> words;
@ -36,53 +51,52 @@ int GetBuiltins(const string& s)
// Go through entire script and iterate through all types to see if line is a // Go through entire script and iterate through all types to see if line is a
// function declaration, then store it with it's value // function declaration, then store it with it's value
for (int lineNum = 0; lineNum < (int)words.size(); lineNum++) for (int lineNum = 0; lineNum < (int)words.size(); lineNum++)
for (int t = 0; t < (int)types.size(); t++)
if (words[lineNum][0] == types[t])
{ {
//Checks if it is function //Checks if it is function
if (words[lineNum][(int)words[lineNum].size() - 1][(int)words[lineNum][(int)words[lineNum].size() - 1].size() - 1] == ')') if (words[lineNum][0] == "func")
{ {
vector<string> functionContents; vector<vector<string>> functionContents;
string functName; string functName = split(words[lineNum][1], '(')[0];
string args = "";
for (int w = 1; w < (int)words[lineNum].size(); w++) { for (int w = 1; w < (int)words[lineNum].size(); w++) {
if (w < (int)words[lineNum].size() - 1) if (w < (int)words[lineNum].size() - 1)
{ {
functName += replace(replace(words[lineNum][w], "(", " "), ")", "") + " "; args += replace(replace(words[lineNum][w], "(", " "), ")", "") + ",";
} }
else else
{ {
functName += replace(replace(words[lineNum][w], "(", " "), ")", ""); args += replace(replace(words[lineNum][w], "(", " "), ")", "");
} }
} }
args = replace(args, functName + ",", "");
functionContents.push_back(vector<string>{args});
int numOfBrackets = 1; int numOfBrackets = 1;
for (int p = lineNum + 2; p < (int)words.size(); p++) for (int p = lineNum + 3; p < (int)words.size(); p++)
{ {
numOfBrackets += countInVector(words[p], "{") - countInVector(words[p], "}"); numOfBrackets += countInVector(words[p], "{") - countInVector(words[p], "}");
if (numOfBrackets == 0) if (numOfBrackets == 0)
break; break;
functionContents.push_back(""); functionContents.push_back(removeTabs(words[p], 1));
for (int w = 0; w < (int)words[p].size(); w++)
{
functionContents[(int)functionContents.size() - 1] += words[p][w] + " ";
} }
}
functionContents = removeTabs(functionContents, 1);
builtinFunctionValues[functName] = functionContents; builtinFunctionValues[functName] = functionContents;
//cout << functName << " is \n" << Vec2Str(functionContents) << endl << endl;
} }
//Checks if it is variable
else else
{ {
if (words[lineNum][0] == "string") if (words[lineNum][0] == "string")
builtinVarVals[words[lineNum][1]] = words[lineNum][3]; builtinVarVals[words[lineNum][1]] = StringRaw(words[lineNum][3]);
else if (words[lineNum][0] == "int") else if (words[lineNum][0] == "int")
builtinVarVals[words[lineNum][1]] = stoi(words[lineNum][3]); builtinVarVals[words[lineNum][1]] = stoi(words[lineNum][3]);
else if (words[lineNum][0] == "float") else if (words[lineNum][0] == "float")
builtinVarVals[words[lineNum][1]] = stof(words[lineNum][3]); builtinVarVals[words[lineNum][1]] = stof(words[lineNum][3]);
else if (words[lineNum][0] == "bool") else if (words[lineNum][0] == "bool")
builtinVarVals[words[lineNum][1]] = stob(words[lineNum][3]); builtinVarVals[words[lineNum][1]] = stob(words[lineNum][3]);
//cout << words[lineNum][1] << " is " << words[lineNum][3] << endl; else
LogWarning("unrecognized type \'" + words[lineNum][0] + "\' on line: " + to_string(lineNum));
} }
} }
@ -113,7 +127,7 @@ any CPPFunction(const string& name, const vector<any>& args)
else if (name == "CPP.System.PrintLine") else if (name == "CPP.System.PrintLine")
cout << AnyAsString(args[0]) << endl; cout << AnyAsString(args[0]) << endl;
else else
LogWarning("CPP function \'" + name + "\' does not exist.") LogWarning("CPP function \'" + name + "\' does not exist.");
return 0; return 0;
} }

View File

@ -7,6 +7,7 @@
#include <stack> #include <stack>
#include "eval.h" #include "eval.h"
#include "strops.h" #include "strops.h"
#include "builtin.h"
using namespace std; using namespace std;
float precedence(const char& op) { float precedence(const char& op) {
@ -26,15 +27,16 @@ float applyOp(const float& a, const float& b, const char& op) {
case '*': return a * b; case '*': return a * b;
case '/': return a / b; case '/': return a / b;
case '^': return pow(a, b); case '^': return pow(a, b);
default: LogWarning("operator \'" << op << "\' does not exist");
} }
string s(1, op);
LogWarning("operator \'" + s + "\' does not exist");
return 0; return 0;
} }
// Function that returns value of // Function that returns value of
// expression after evaluation. // expression after evaluation.
float evaluate(const string& t) { float evaluate(const string& t) {
tokens = replace(t, " ", ""); string tokens = replace(t, " ", "");
float i; float i;

View File

@ -34,7 +34,7 @@ public:
bool OnUserUpdate(float fElapsedTime) override bool OnUserUpdate(float fElapsedTime) override
{ {
ExecuteFunction("Update", vector<string> {""}, -1); ExecuteFunction("Update", vector<any> {});
// Called once per frame // Called once per frame
//for (int x = 0; x < ScreenWidth(); x++) //for (int x = 0; x < ScreenWidth(); x++)

View File

@ -3,7 +3,8 @@
#define MAIN_H #define MAIN_H
using namespace std; using namespace std;
using namespace boost;
any ExecuteFunction(const string functionName, const vector<any> inputVarVals) any ExecuteFunction(const string functionName, const vector<any> inputVarVals);
#endif #endif

View File

@ -4,10 +4,82 @@
#include <regex> #include <regex>
#include <limits> #include <limits>
#include "strops.h" #include "strops.h"
#include "builtin.h"
using namespace std; using namespace std;
const string WHITESPACE = " \n\r\t\f\v"; const string WHITESPACE = " \n\r\t\f\v";
bool isNumber(const string& str)
{
for (char const& c : str) {
if (isdigit(c) == 0 && c != '.') return false;
}
return true;
}
bool stob(string str)
{
transform(str.begin(), str.end(), str.begin(), ::tolower);
istringstream is(str);
bool b;
is >> boolalpha >> b;
return b;
}
string StringRaw(const string& s)
{
string str = trim(s);
if (str.size() < 3)
return str;
string withoutQuotes;
if (str[0] != '\"')
withoutQuotes += str[0];
withoutQuotes += str.substr(1, str.size() - 2);
if (str[str.size() - 1] != '\"')
withoutQuotes += str[str.size() - 1];
return withoutQuotes;
}
string Quoted(const string& s)
{
string str = trim(s);
string withQuotes;
if (str[0] != '\"')
withQuotes += '\"';
withQuotes += str;
if (str[str.size() - 1] != '\"')
withQuotes += '\"';
return withQuotes;
}
string RMParenthesis(const string& s)
{
string str = trim(s);
string withoutParenthesis;
if (str[0] != '(')
withoutParenthesis += str[0];
withoutParenthesis += str.substr(1, str.size() - 2);
if (str[str.size() - 1] != ')')
withoutParenthesis += str[str.size() - 1];
return withoutParenthesis;
}
string ltrim(const string& s) string ltrim(const string& s)
{ {
size_t start = s.find_first_not_of(WHITESPACE); size_t start = s.find_first_not_of(WHITESPACE);
@ -24,7 +96,7 @@ string trim(const string& s) {
return rtrim(ltrim(s)); return rtrim(ltrim(s));
} }
vector<string> split(string str, char del) { vector<string> split(const string& str, const char& del) {
if (count(str, del) == 0) if (count(str, del) == 0)
return vector<string>{str}; return vector<string>{str};
@ -51,7 +123,7 @@ vector<string> split(string str, char del) {
return splitWords; return splitWords;
} }
int count(string str, char ch) { int count(const string& str, const char& ch) {
int cnt = 0; int cnt = 0;
for (int i = 0; i < (int)str.size(); i++) for (int i = 0; i < (int)str.size(); i++)
@ -61,7 +133,7 @@ int count(string str, char ch) {
return cnt; return cnt;
} }
int countNoOverlap(string str, char ch1, char ch2) { int countNoOverlap(const string& str, const char& ch1, const char& ch2) {
int cnt = 0; int cnt = 0;
bool waitingForClose = false; bool waitingForClose = false;
@ -80,7 +152,7 @@ int countNoOverlap(string str, char ch1, char ch2) {
return cnt; return cnt;
} }
int indexInStr(string str, char ch) { int indexInStr(const string& str, const char& ch) {
for (int i = 0; i < (int)str.size(); i++) for (int i = 0; i < (int)str.size(); i++)
if (str[i] == ch) if (str[i] == ch)
@ -89,7 +161,7 @@ int indexInStr(string str, char ch) {
return -1; return -1;
} }
int charIndexInVec(vector<string> str, char ch) { int charIndexInVec(const vector<string>& str, const char& ch) {
for (int i = 0; i < (int)str.size(); i++) for (int i = 0; i < (int)str.size(); i++)
for (int w = 0; w < (int)str[i].size(); w++) for (int w = 0; w < (int)str[i].size(); w++)
@ -99,7 +171,7 @@ int charIndexInVec(vector<string> str, char ch) {
return -1; return -1;
} }
int countInVector(vector<string> str, string ch) { int countInVector(const vector<string>& str, const string& ch) {
int cnt = 0; int cnt = 0;
for (int i = 0; i < (int)str.size(); i++) for (int i = 0; i < (int)str.size(); i++)
@ -109,7 +181,7 @@ int countInVector(vector<string> str, string ch) {
return cnt; return cnt;
} }
string Vec2Str(vector<string> str) { string Vec2Str(const vector<string>& str) {
string outStr; string outStr;
for (int i = 0; i < (int)str.size(); i++) for (int i = 0; i < (int)str.size(); i++)
@ -118,7 +190,7 @@ string Vec2Str(vector<string> str) {
return outStr; return outStr;
} }
vector<string> removeTabs(vector<string> str, int amnt) { vector<string> removeTabs(const vector<string>& str, const int& amnt) {
vector<string> newStr; vector<string> newStr;
for (int i = 0; i < (int)str.size(); i++) for (int i = 0; i < (int)str.size(); i++)
@ -135,7 +207,7 @@ vector<string> removeTabs(vector<string> str, int amnt) {
return newStr; return newStr;
} }
vector<string> rangeInVec(vector<string> str, int min, int max) { vector<string> rangeInVec(const vector<string>& str, const int& min, int max) {
if (max == -1) if (max == -1)
max = (int)str.size(); max = (int)str.size();
@ -147,7 +219,7 @@ vector<string> rangeInVec(vector<string> str, int min, int max) {
return newStr; return newStr;
} }
string rangeInStr(string str, int min, int max) { string rangeInStr(const string& str, const int& min, int max) {
if (max == -1) if (max == -1)
max = (int)str.size(); max = (int)str.size();
@ -159,7 +231,7 @@ string rangeInStr(string str, int min, int max) {
return newStr; return newStr;
} }
string unWrapVec(vector<string> vec) { string unWrapVec(const vector<string>& vec) {
string newStr; string newStr;
for (int i = 0; i < (int)vec.size(); i++) for (int i = 0; i < (int)vec.size(); i++)
@ -172,7 +244,7 @@ string unWrapVec(vector<string> vec) {
return newStr; return newStr;
} }
float floatval(string s) float floatval(const string& s)
{ {
float outfloat; float outfloat;
@ -188,7 +260,7 @@ float floatval(string s)
return outfloat; return outfloat;
} }
string replace(string str, string strToReplace, string replaceWith) { string replace(const string& str, const string& strToReplace, const string& replaceWith) {
string newStr; string newStr;
string savedLetters; string savedLetters;

View File

@ -3,6 +3,16 @@
using namespace std; using namespace std;
bool isNumber(const string& str);
bool stob(string str);
string StringRaw(const string& s);
string Quoted(const string& s);
string RMParenthesis(const string& s);
string ltrim(const string& s); string ltrim(const string& s);
string rtrim(const string& s); string rtrim(const string& s);