diff --git a/Slang/Main.cpp b/Slang/Main.cpp index 0ba4924..b408b85 100644 --- a/Slang/Main.cpp +++ b/Slang/Main.cpp @@ -6,6 +6,8 @@ #include #include #include +#include "eval.h" +#include "strops.h" using namespace std; @@ -51,231 +53,14 @@ bool isNumber(const string& str) return true; } -const string WHITESPACE = " \n\r\t\f\v"; - -string ltrim(const string& s) -{ - size_t start = s.find_first_not_of(WHITESPACE); - return (start == string::npos) ? "" : s.substr(start); -} - -string rtrim(const string& s) -{ - size_t end = s.find_last_not_of(WHITESPACE); - return (end == string::npos) ? "" : s.substr(0, end + 1); -} - -string trim(const string& s) { - return rtrim(ltrim(s)); -} - -vector split(string str, char del) { - // declaring temp string to store the curr "word" upto del - string temp = ""; - vector splitWords; - - for (int i = 0; i < (int)str.size(); i++) - { - // If cur char is not del, then append it to the cur "word", otherwise - // you have completed the word, print it, and start a new word. - if (str[i] != del) - { - temp += str[i]; - } - else - { - splitWords.push_back(temp); - temp = ""; - } - } - splitWords.push_back(temp); - - return splitWords; -} - -int count(string str, char ch) { - int cnt = 0; - - for (int i = 0; i < (int)str.size(); i++) - if (str[i] == ch) - cnt++; - - return cnt; -} - -int countNoOverlap(string str, char ch1, char ch2) { - int cnt = 0; - - bool waitingForClose = false; - - for (int i = 0; i < (int)str.size(); i++) - { - if (str[i] == ch1) - waitingForClose = true; - else if (str[i] == ch2 && waitingForClose == true) - { - cnt++; - waitingForClose = false; - } - } - - return cnt; -} - -int indexInStr(string str, char ch) { - - for (int i = 0; i < (int)str.size(); i++) - if (str[i] == ch) - return i; - - return -1; -} - -int charIndexInVec(vector str, char ch) { - - for (int i = 0; i < (int)str.size(); i++) - for (int w = 0; w < (int)str[i].size(); w++) - if (str[i][w] == ch) - return i; - - return -1; -} - -int countInVector(vector str, string ch) { - int cnt = 0; - - for (int i = 0; i < (int)str.size(); i++) - if (str[i] == ch) - cnt++; - - return cnt; -} - -string Vec2Str(vector str) { - string outStr; - - for (int i = 0; i < (int)str.size(); i++) - outStr += str[i] + "\n"; - - return outStr; -} - -vector removeTabs(vector str, int amnt) { - vector newStr; - - for (int i = 0; i < (int)str.size(); i++) - { - newStr.push_back(""); - - for (int c = 0; c < (int)str[i].size(); c++) - { - if (str[i][c] != '\t' || c >= amnt) - newStr[i] += str[i][c]; - } - } - - return newStr; -} - -vector rangeInVec(vector str, int min, int max) { - if (max == -1) - max = (int)str.size(); - - vector newStr; - - for (int i = min; i < (int)str.size() && i < max; i++) - newStr.push_back(str[i]); - - return newStr; -} - -string rangeInStr(string str, int min, int max) { - if (max == -1) - max = (int)str.size(); - - string newStr; - - for (int i = min; i < (int)str.size() && i < max; i++) - newStr += str[i]; - - return newStr; -} - -string unWrapVec(vector vec) { - string newStr; - - for (int i = 0; i < (int)vec.size(); i++) - { - newStr += vec[i]; - if (i != (int)vec.size() - 1) - newStr += " "; - } - - return newStr; -} - -float floatval(string s) -{ - float outfloat; - - if (s == "inf") - outfloat = numeric_limits::max(); - else if (s == "-inf") - outfloat = -numeric_limits::max(); - else if (s == "") - outfloat = 0; - else - outfloat = stof(s); - - return outfloat; -} - -string replace(string str, string strToReplace, string replaceWith) { - string newStr; - string savedLetters;; - - int sameLetters = 0; - int startReplaceIndex = 0; - for (int i = 0; i < (int)str.size(); i++) - { - if (str[i] == strToReplace[sameLetters]) - { - savedLetters += str[i]; - if (sameLetters == 0) - startReplaceIndex = i; - sameLetters++; - - if ((int)strToReplace.size() == sameLetters) - { - //cout << "replaced " << "\"" << strToReplace << "\"" << startReplaceIndex << endl; - newStr += replaceWith; - sameLetters = 0; - savedLetters = ""; - } - } - else - { - newStr += savedLetters + str[i]; - sameLetters = 0; - savedLetters = ""; - } - } - - return newStr; -} - string AddItem(string varName, string variableContent, string addItem) { string typ = split(varName, ' ')[0]; if (typ == "int" || typ == "float" || typ == "double" && isNumber(addItem)) - { return to_string(floatval(to_string(floatval(variableContent) + floatval(addItem)))); - } else - { return variableContent + addItem; - } } string GetRealValue(string varName, vector& variables, vector& variableVals) @@ -298,28 +83,6 @@ string GetRealValue(string varName, vector& variables, vector& v isVar = true; } - //if ((typ == "int" || typ == "float" || typ == "double") && isVar) - //{ - // // Checks against global vars - // for (int v = 0; v < (int)globalVariables.size(); v++) - // if (varName == split(globalVariables[v], ' ')[1]) - // { - // if (globalVariableValues[v] == "inf") - // globalVariableValues[v] = 2147483646; - // if (globalVariableValues[v] == "-inf") - // globalVariableValues[v] = -2147483646; - // } - // // Checks against local vars - // for (int v = 0; v < (int)variables.size(); v++) - // if (varName == split(variables[v], ' ')[1]) - // { - // if (variableVals[v] == "inf") - // variableVals[v] = 2147483646; - // if (variableVals[v] == "-inf") - // variableVals[v] = -2147483646; - // } - //} - // If it is a var if (isVar) { @@ -339,39 +102,83 @@ string GetRealValue(string varName, vector& variables, vector& v else if (!isVar && count(varName, '\"') > 0) { string withoutQuotes; + for (int ch = 1; ch < (int)varName.size() - 1; ch++) - { withoutQuotes += varName[ch]; - } + return withoutQuotes; } return varName; } +string EvalExpression(string expression, vector& variables, vector& variableVals) +{ + string newExpression = ""; + + for (int i = 0; i < expression.size(); i++) + { + if (isalpha(expression[i])) + { + string name = ""; + + string val = ""; + + while (i < expression.size() && isalpha(expression[i])) + { + name += expression[i]; + + i++; + } + + newExpression += GetRealValue(val, variables, variableVals); + + i--; + } + else + { + newExpression += expression[i]; + } + } + + vector trimmedVersion = split(replace(replace(newExpression, "(", ""), ")", ""), '+'); + bool addStrings = false; + string newStr = ""; + for (int i = 0; i < newExpression.size(); i++) + if (isalpha(expression[i])) + { + addStrings = true; + break; + } + if (addStrings) + { + for (int i = 0; i < trimmedVersion.size(); i++) + newStr += trimmedVersion[i]; + + return newStr; + } + else + return to_string(evaluate(newExpression)); +} + bool BooleanLogic(string valA, string determinant, string valB, vector& variables, vector& variableVals) { string valARealValue = GetRealValue(valA, variables, variableVals); string valBRealValue = GetRealValue(valB, variables, variableVals); - if (determinant == "==") { + if (determinant == "==") return valARealValue == valBRealValue; - } - if (determinant == "!=") { + if (determinant == "!=") return valARealValue != valBRealValue; - } - if (determinant == ">=") { + if (determinant == ">=") return floatval(valARealValue) >= floatval(valBRealValue); - } - if (determinant == "<=") { + if (determinant == "<=") return floatval(valARealValue) <= floatval(valBRealValue); - } - if (determinant == ">") { + if (determinant == ">") return floatval(valARealValue) > floatval(valBRealValue); - } - if (determinant == "<") { + if (determinant == "<") return floatval(valARealValue) < floatval(valBRealValue); - } + return false; } @@ -383,15 +190,15 @@ int evalEqu(vector str, vector& variables, vector& varia if (str[0] == split(variables[v], ' ')[1]) { if (str[1] == "=") - variableValues[v] = GetRealValue(str[2], variables, variableValues); + variableValues[v] = EvalExpression("(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); else if (str[1] == "+=") - variableValues[v] = AddItem(variables[v], variableValues[v], GetRealValue(str[2], variables, variableValues)); + variableValues[v] = EvalExpression(variableValues[v] + "+(" + unWrapVec(vector(str.begin()+2, str.end())) + ")", variables, variableValues); else if (str[1] == "-=") - variableValues[v] = to_string(floatval(variableValues[v]) - floatval(GetRealValue(str[2], variables, variableValues))); + variableValues[v] = EvalExpression(variableValues[v] + "-(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); else if (str[1] == "*=") - variableValues[v] = to_string(floatval(variableValues[v]) * floatval(GetRealValue(str[2], variables, variableValues))); + variableValues[v] = EvalExpression(variableValues[v] + "*(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); else if (str[1] == "/=") - variableValues[v] = to_string(floatval(variableValues[v]) / floatval(GetRealValue(str[2], variables, variableValues))); + variableValues[v] = EvalExpression(variableValues[v] + "/(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); //cout << words[lineNum][1] << " is " << words[lineNum][3] << endl << endl; return 0; @@ -403,15 +210,15 @@ int evalEqu(vector str, vector& variables, vector& varia if (str[0] == split(globalVariables[v], ' ')[1]) { if (str[1] == "=") - globalVariableValues[v] = GetRealValue(str[2], variables, variableValues); + globalVariableValues[v] = EvalExpression("(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); else if (str[1] == "+=") - globalVariableValues[v] = AddItem(variables[v], globalVariableValues[v], GetRealValue(str[2], variables, variableValues)); + globalVariableValues[v] = EvalExpression(variableValues[v] + "+(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); else if (str[1] == "-=") - globalVariableValues[v] = to_string(floatval(globalVariableValues[v]) - floatval(GetRealValue(str[2], variables, variableValues))); + globalVariableValues[v] = EvalExpression(variableValues[v] + "-(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); else if (str[1] == "*=") - globalVariableValues[v] = to_string(floatval(globalVariableValues[v]) * floatval(GetRealValue(str[2], variables, variableValues))); + globalVariableValues[v] = EvalExpression(variableValues[v] + "*(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); else if (str[1] == "/=") - globalVariableValues[v] = to_string(floatval(globalVariableValues[v]) / floatval(GetRealValue(str[2], variables, variableValues))); + globalVariableValues[v] = EvalExpression(variableValues[v] + "/(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); //cout << words[lineNum][1] << " is " << words[lineNum][3] << endl << endl; return 0; @@ -419,40 +226,11 @@ int evalEqu(vector str, vector& variables, vector& varia } } -string PEMDAS(string equ, vector& variables, vector& variableValues) -{ - if (split(equ, ',').size() == 1) - return equ; - - int parenthesisSetsCount = countNoOverlap(equ, '(', ')'); - - vector equationWords = split(equ, ' '); - for (int s = 0; s < parenthesisSetsCount; s++) - { - int startOfNextParenthesis = 0; - int numofParenthesis = 0; - vector insideParenthesis; - for (int p = startOfNextParenthesis; p < (int)equationWords.size(); p++) - { - numofParenthesis += count(equationWords[p], '(') - count(equationWords[p], ')'); - if (numofParenthesis == 0) - { - startOfNextParenthesis = indexInStr(equationWords[charIndexInVec(equationWords, '(')], '('); - break; - } - insideParenthesis.push_back(""); - for (int w = 0; w < (int)equationWords[p].size(); w++) - { - insideParenthesis[(int)insideParenthesis.size() - 1] += equationWords[p][w] + " "; - } - } - - equ = replace(equ, "(" + unWrapVec(insideParenthesis) + ")", PEMDAS(unWrapVec(insideParenthesis), variables, variableValues)); - } -} - int ProcessLine(vector> words, int lineNum, vector& variables, vector& variableValues) { + if (words[lineNum][0][0] == '/' && words[lineNum][0][1] == '/') + return 0; + if (words[lineNum][0] == "print") { cout << GetRealValue(words[lineNum][1], variables, variableValues) << endl; return 0; @@ -480,7 +258,7 @@ int ProcessLine(vector> words, int lineNum, vector& varia if (words[lineNum][1] == split(variables[v], ' ')[1]) { vector inputs = { words[lineNum][1], words[lineNum][2], words[lineNum][3] }; - evalEqu(inputs, variables, variableValues); + evalEqu(vector(words[lineNum].begin() + 1, words[lineNum].end()), variables, variableValues); return 0; } @@ -489,7 +267,7 @@ int ProcessLine(vector> words, int lineNum, vector& varia //Checks if it is variable variables.push_back(words[lineNum][0] + " " + words[lineNum][1]); variableValues.push_back(GetRealValue((string)words[lineNum][3], variables, variableValues)); - cout << variables[(int)variables.size() - 1] << " is " << variableValues[(int)variableValues.size() -1] << endl; + cout << variables[(int)variables.size() - 1] << " is " << variableValues[(int)variableValues.size() - 1] << endl; return 0; } } diff --git a/Slang/Slang.vcxproj b/Slang/Slang.vcxproj index da7153a..3dae791 100644 --- a/Slang/Slang.vcxproj +++ b/Slang/Slang.vcxproj @@ -140,13 +140,17 @@ + + + + - + diff --git a/Slang/Slang.vcxproj.filters b/Slang/Slang.vcxproj.filters index 78144ec..baa8bb7 100644 --- a/Slang/Slang.vcxproj.filters +++ b/Slang/Slang.vcxproj.filters @@ -18,13 +18,25 @@ Source Files + + Source Files + + + Source Files + Header Files + + Source Files + + + Source Files + - + \ No newline at end of file diff --git a/Slang/eval.cpp b/Slang/eval.cpp new file mode 100644 index 0000000..86b439b --- /dev/null +++ b/Slang/eval.cpp @@ -0,0 +1,152 @@ +// CPP program to evaluate a given expression +#include +#include +#include +#include +#include +#include +#include "eval.h" +using namespace std; + +float precedence(char op) { + if (op == '+' || op == '-') + return 1; + if (op == '*' || op == '/') + return 2; + return 0; +} + +float applyOp(float a, float b, char op) { + switch (op) { + case '+': return a + b; + case '-': return a - b; + case '*': return a * b; + case '/': return a / b; + } +} + +// Function that returns value of +// expression after evaluation. +float evaluate(string tokens) { + float i; + + stack values; + stack ops; + + for (i = 0; i < tokens.length(); i++) { + + // Current token is a whitespace, + // skip it. + if (tokens[i] == ' ') + continue; + + // Current token is an opening + // brace, push it to 'ops' + else if (tokens[i] == '(') + { + ops.push(tokens[i]); + } + + // Current token is a number, push + // it to stack for numbers. + else if (isdigit(tokens[i])) { + float val = 0; + + bool encounteredDecimal = false; + int decPlaces = 0; + // There may be more than one + // digits in number. + while (i < tokens.length() && (isdigit(tokens[i]) || tokens[i] == '.')) + { + if (tokens[i] == '.') + { + encounteredDecimal = true; + i++; + decPlaces++; + continue; + } + + if (encounteredDecimal == true) + { + val = (val)+(tokens[i] - '0') / pow(10.0f, decPlaces); + decPlaces++; + } + else + val = (val * 10) + (tokens[i] - '0'); + + i++; + } + + values.push(val); + + i--; + } + // Closing brace encountered, solve + // entire brace. + else if (tokens[i] == ')') + { + while (!ops.empty() && ops.top() != '(') + { + float val2 = values.top(); + values.pop(); + + float val1 = values.top(); + values.pop(); + + char op = ops.top(); + ops.pop(); + + values.push(applyOp(val1, val2, op)); + } + + if (!ops.empty()) + ops.pop(); + } + // Current token is an operator. + else + { + while (!ops.empty() && precedence(ops.top()) + >= precedence(tokens[i])) { + float val2 = values.top(); + values.pop(); + + float val1 = values.top(); + values.pop(); + + char op = ops.top(); + ops.pop(); + + values.push(applyOp(val1, val2, op)); + } + + ops.push(tokens[i]); + } + } + + // Entire expression has been parsed at this + // point, apply remaining ops to remaining + // values. + while (!ops.empty()) { + float val2 = values.top(); + values.pop(); + + float val1 = values.top(); + values.pop(); + + char op = ops.top(); + ops.pop(); + + values.push(applyOp(val1, val2, op)); + } + + return values.top(); +} + +// Used to test evaluator +//int main() { +// cout << evaluate("10 + 2 * 6") << "\n"; +// cout << evaluate("100 * 2 + 12") << "\n"; +// cout << evaluate("100 * ( 2 + 12 )") << "\n"; +// cout << evaluate("0.05*0.05"); +// return 0; +//} diff --git a/Slang/eval.h b/Slang/eval.h new file mode 100644 index 0000000..8650d41 --- /dev/null +++ b/Slang/eval.h @@ -0,0 +1,6 @@ +#ifndef EVAL_H +#define EVAL_H + +float evaluate(std::string tokens); + +#endif \ No newline at end of file diff --git a/Slang/processLine.cpp b/Slang/processLine.cpp deleted file mode 100644 index 345471c..0000000 --- a/Slang/processLine.cpp +++ /dev/null @@ -1,155 +0,0 @@ -//#include -//#include -//#include -//#include -//#include "Main.cpp" -// -//using namespace std; -// -//int ProcessLine(vector> words, int l, vector variables, vector variableValues) -//{ -// if (words[l][0] == "print") { -// cout << GetRealValue(words[l][1], variables, variableValues) << endl; -// return 0; -// } -// -// // Iterate through all functions -// for (int t = 0; t < (int)functions.size(); t++) -// { -// if (words[l][0] == split(functions[t], ' ')[0]) -// { -// ExecuteFunction(words[l][0], rangeInVec(words[l], 1, -1)); -// return 0; -// } -// } -// -// // First iterate through all types to see if line -// // is a variable then store it with it's value -// for (int t = 0; t < (int)types.size(); t++) -// { -// if (words[l][0] == types[t]) -// { -// //Checks if it is variable -// variables.push_back(words[l][1]); -// variableValues.push_back((string)words[l][3]); -// //cout << words[l][1] << " is " << words[l][3] << endl << endl; -// return 0; -// } -// } -// // Second, iterate all existing local variable names -// for (int v = 0; v < (int)variables.size(); v++) -// { -// if (words[l][0] == variables[v]) -// { -// if (words[l][1] == "=") -// variableValues[v] = words[l][2]; -// else if (words[l][1] == "+=") -// variableValues[v] = AddItem(variableValues[v], words[l][2]); -// else if (words[l][1] == "-=") -// variableValues[v] = to_string(stof(variableValues[v]) - stof(words[l][2])); -// else if (words[l][1] == "*=") -// variableValues[v] = to_string(stof(variableValues[v]) * stof(words[l][2])); -// else if (words[l][1] == "/=") -// variableValues[v] = to_string(stof(variableValues[v]) / stof(words[l][2])); -// -// //cout << words[l][1] << " is " << words[l][3] << endl << endl; -// return 0; -// } -// } -// // Third, iterate all existing global variable names -// for (int v = 0; v < (int)globalVariables.size(); v++) -// { -// if (words[l][0] == globalVariables[v]) -// { -// if (words[l][1] == "=") -// globalVariableValues[v] = words[l][2]; -// else if (words[l][1] == "+=") -// globalVariableValues[v] = AddItem(globalVariableValues[v], words[l][2]); -// else if (words[l][1] == "-=") -// globalVariableValues[v] = to_string(stof(globalVariableValues[v]) - stof(words[l][2])); -// else if (words[l][1] == "*=") -// globalVariableValues[v] = to_string(stof(globalVariableValues[v]) * stof(words[l][2])); -// else if (words[l][1] == "/=") -// globalVariableValues[v] = to_string(stof(globalVariableValues[v]) / stof(words[l][2])); -// -// //cout << words[l][1] << " is " << words[l][3] << endl << endl; -// return 0; -// } -// } -// // Gathers while loop contents -// if (words[l][0] == "while") -// { -// vector whileContents; -// vector whileParameters; -// -// for (int w = 1; w < (int)words[l].size(); w++) -// whileParameters.push_back(words[l][w]); -// -// int numOfBrackets = 1; -// for (int p = l + 2; p < (int)words.size(); p++) -// { -// numOfBrackets += countInVector(words[p], "{") - countInVector(words[p], "}"); -// if (numOfBrackets == 0) -// break; -// whileContents.push_back(""); -// for (int w = 0; w < (int)words[p].size(); w++) -// { -// whileContents[(int)whileContents.size() - 1] += words[p][w] + " "; -// } -// } -// whileContents = removeTabs(whileContents, 1); -// -// vector> words; -// for (int i = 0; i < (int)whileContents.size(); i++) -// words.push_back(split(whileContents[i], ' ')); -// -// while (BooleanLogic(whileParameters[0], whileParameters[1], whileParameters[2], variables, variableValues)) -// { -// //Iterate through all lines in while loop -// for (int l = 0; l < (int)whileContents.size(); l++) -// { -// ProcessLine(words, l, variables, variableValues); -// } -// } -// return 0; -// } -// // Gathers if statement contents -// if (words[l][0] == "if") -// { -// vector ifContents; -// vector ifParameters; -// -// for (int w = 1; w < (int)words[l].size(); w++) -// ifParameters.push_back(words[l][w]); -// -// int numOfBrackets = 1; -// for (int p = l + 2; p < (int)words.size(); p++) -// { -// numOfBrackets += countInVector(words[p], "{") - countInVector(words[p], "}"); -// if (numOfBrackets == 0) -// break; -// ifContents.push_back(""); -// for (int w = 0; w < (int)words[p].size(); w++) -// { -// ifContents[(int)ifContents.size() - 1] += words[p][w] + " "; -// } -// } -// ifContents = removeTabs(ifContents, 1); -// -// vector> words; -// for (int i = 0; i < (int)ifContents.size(); i++) -// words.push_back(split(ifContents[i], ' ')); -// -// if (BooleanLogic(ifParameters[0], ifParameters[1], ifParameters[2], variables, variableValues)) -// { -// //Iterate through all lines in while loop -// for (int l = 0; l < (int)ifContents.size(); l++) -// { -// ProcessLine(words, l, variables, variableValues); -// } -// } -// return 0; -// } -// -// return 0; -//} \ No newline at end of file diff --git a/Slang/script.slg b/Slang/script.slg index 658317b..428611d 100644 --- a/Slang/script.slg +++ b/Slang/script.slg @@ -25,14 +25,11 @@ void Main(string input, int in2) while x < 100 { - k = a - k += b - a = b - b = k - + k = 10*4/3+(6.2-5.8) + print k string str = x - str += "::" + //str += "::" str += k x += 10 diff --git a/Slang/strops.cpp b/Slang/strops.cpp new file mode 100644 index 0000000..9f21f36 --- /dev/null +++ b/Slang/strops.cpp @@ -0,0 +1,220 @@ +#include +#include +#include +#include +#include +#include "strops.h" +using namespace std; + +const string WHITESPACE = " \n\r\t\f\v"; + +string ltrim(const string& s) +{ + size_t start = s.find_first_not_of(WHITESPACE); + return (start == string::npos) ? "" : s.substr(start); +} + +string rtrim(const string& s) +{ + size_t end = s.find_last_not_of(WHITESPACE); + return (end == string::npos) ? "" : s.substr(0, end + 1); +} + +string trim(const string& s) { + return rtrim(ltrim(s)); +} + +vector split(string str, char del) { + // declaring temp string to store the curr "word" upto del + string temp = ""; + vector splitWords; + + for (int i = 0; i < (int)str.size(); i++) + { + // If cur char is not del, then append it to the cur "word", otherwise + // you have completed the word, print it, and start a new word. + if (str[i] != del) + { + temp += str[i]; + } + else + { + splitWords.push_back(temp); + temp = ""; + } + } + splitWords.push_back(temp); + + return splitWords; +} + +int count(string str, char ch) { + int cnt = 0; + + for (int i = 0; i < (int)str.size(); i++) + if (str[i] == ch) + cnt++; + + return cnt; +} + +int countNoOverlap(string str, char ch1, char ch2) { + int cnt = 0; + + bool waitingForClose = false; + + for (int i = 0; i < (int)str.size(); i++) + { + if (str[i] == ch1) + waitingForClose = true; + else if (str[i] == ch2 && waitingForClose == true) + { + cnt++; + waitingForClose = false; + } + } + + return cnt; +} + +int indexInStr(string str, char ch) { + + for (int i = 0; i < (int)str.size(); i++) + if (str[i] == ch) + return i; + + return -1; +} + +int charIndexInVec(vector str, char ch) { + + for (int i = 0; i < (int)str.size(); i++) + for (int w = 0; w < (int)str[i].size(); w++) + if (str[i][w] == ch) + return i; + + return -1; +} + +int countInVector(vector str, string ch) { + int cnt = 0; + + for (int i = 0; i < (int)str.size(); i++) + if (str[i] == ch) + cnt++; + + return cnt; +} + +string Vec2Str(vector str) { + string outStr; + + for (int i = 0; i < (int)str.size(); i++) + outStr += str[i] + "\n"; + + return outStr; +} + +vector removeTabs(vector str, int amnt) { + vector newStr; + + for (int i = 0; i < (int)str.size(); i++) + { + newStr.push_back(""); + + for (int c = 0; c < (int)str[i].size(); c++) + { + if (str[i][c] != '\t' || c >= amnt) + newStr[i] += str[i][c]; + } + } + + return newStr; +} + +vector rangeInVec(vector str, int min, int max) { + if (max == -1) + max = (int)str.size(); + + vector newStr; + + for (int i = min; i < (int)str.size() && i < max; i++) + newStr.push_back(str[i]); + + return newStr; +} + +string rangeInStr(string str, int min, int max) { + if (max == -1) + max = (int)str.size(); + + string newStr; + + for (int i = min; i < (int)str.size() && i < max; i++) + newStr += str[i]; + + return newStr; +} + +string unWrapVec(vector vec) { + string newStr; + + for (int i = 0; i < (int)vec.size(); i++) + { + newStr += vec[i]; + if (i != (int)vec.size() - 1) + newStr += " "; + } + + return newStr; +} + +float floatval(string s) +{ + float outfloat; + + if (s == "inf") + outfloat = numeric_limits::max(); + else if (s == "-inf") + outfloat = -numeric_limits::max(); + else if (s == "") + outfloat = 0; + else + outfloat = stof(s); + + return outfloat; +} + +string replace(string str, string strToReplace, string replaceWith) { + string newStr; + string savedLetters;; + + int sameLetters = 0; + int startReplaceIndex = 0; + for (int i = 0; i < (int)str.size(); i++) + { + if (str[i] == strToReplace[sameLetters]) + { + savedLetters += str[i]; + if (sameLetters == 0) + startReplaceIndex = i; + sameLetters++; + + if ((int)strToReplace.size() == sameLetters) + { + //cout << "replaced " << "\"" << strToReplace << "\"" << startReplaceIndex << endl; + newStr += replaceWith; + sameLetters = 0; + savedLetters = ""; + } + } + else + { + newStr += savedLetters + str[i]; + sameLetters = 0; + savedLetters = ""; + } + } + + return newStr; +} \ No newline at end of file diff --git a/Slang/strops.h b/Slang/strops.h new file mode 100644 index 0000000..79910c4 --- /dev/null +++ b/Slang/strops.h @@ -0,0 +1,38 @@ +#ifndef STROPS_H +#define STROPS_H + +using namespace std; + +string ltrim(const string& s); + +string rtrim(const string& s); + +string trim(const string& s); + +vector split(string str, char del); + +int count(string str, char ch); + +int countNoOverlap(string str, char ch1, char ch2); + +int indexInStr(string str, char ch); + +int charIndexInVec(vector str, char ch); + +int countInVector(vector str, string ch); + +string Vec2Str(vector str); + +vector removeTabs(vector str, int amnt); + +vector rangeInVec(vector str, int min, int max); + +string rangeInStr(string str, int min, int max); + +string unWrapVec(vector vec); + +float floatval(string s); + +string replace(string str, string strToReplace, string replaceWith); + +#endif \ No newline at end of file