Originally Posted by
tabstop
Except, of course, it does no such thing. (I took out the InStringLiteral thing rather than write my own. I hope you don't mind.)
Code:
#include <iostream>
#include <string>
using namespace std;
void properlyFormatScript(string& scriptStr) {
cout<< "Just entered properlyFormatScript()" <<endl;
cout<< "scriptStr.size() is: " << scriptStr.size() <<endl;
//cin.get();
for (size_t i = 0; i < scriptStr.size(); i++) {
cout<< "i is: " << i << endl;
if ((scriptStr.at(i) == '{')) {
if (i < scriptStr.size() - 2) {
if (scriptStr.at(i+1) != '\n' && scriptStr.at(i+2) != '\n') {
scriptStr.insert(i+1, "\n\n");
}
}
else
scriptStr.append("\n\n");
}
else if ((scriptStr.at(i) == ';')) {
if (i < scriptStr.size() - 1) {
if (scriptStr.at(i+1) != '\n') {
scriptStr.insert(i + 1, "\n");
}
}
else
scriptStr.append("\n\n");
}
else if ((scriptStr.at(i) == '}')) {
if (scriptStr.at(i-1) != '\n' && scriptStr.at(i-2) != '\n') {
scriptStr.insert(i - 1, "\n\n");
}
}
}
}
int main() {
string foo("Something{withsome;bracesand;semicolons}init;");
cout << foo << endl;
properlyFormatScript(foo);
cout << foo << endl;
return 0;
}
I haven't tested your code yet (though I know for a fact that it was definitely taking a long time to go through a loop just like yours on my computer, and I just terminated the program after waiting about 30 seconds, and changed the code to use temp_str instead). Anyway, I just fixed my problem by using this code instead:
Code:
void C_script_operations::properlyFormatScript(string& scriptStr) {
cout<< "Just entered properlyFormatScript()" <<endl;
cout<< "scriptStr.size() is: " << scriptStr.size() <<endl;
//cin.get();
string temp_str = scriptStr;
bool this_char_was_already_checked = false;
for (size_t i = 0; i < scriptStr.size(); i++) {
cout<< "i is: " << i << endl;
if ((temp_str.at(i) == '>') && (!charIsInLiteralString(temp_str, temp_str.at(i), i))
&& (!this_char_was_already_checked)) {
if (i < temp_str.size() - 1) {
if (temp_str.at(i+1) != '\n' && temp_str.at(i+2) != '\n') {
temp_str.insert(i+1, "\n");
this_char_was_already_checked = true;
}
}
else
temp_str.append("\n\n");
}
else if ((temp_str.at(i) == '{') && (!charIsInLiteralString(temp_str, temp_str.at(i), i))
&& (!this_char_was_already_checked)) {
if (i < temp_str.size() - 2) {
if (temp_str.at(i+1) != '\n' && temp_str.at(i+2) != '\n') {
temp_str.insert(i+1, "\n\n");
this_char_was_already_checked = true;
}
}
else
temp_str.append("\n\n");
}
else if ((temp_str.at(i) == ';') && (!charIsInLiteralString(temp_str, temp_str.at(i), i))
&& (!this_char_was_already_checked)) {
if (i < temp_str.size() - 1) {
if (temp_str.at(i+1) != '\n') {
temp_str.insert(i + 1, "\n");
this_char_was_already_checked = true;
}
}
else
temp_str.append("\n\n");
}
else if ((temp_str.at(i) == '}') && (!charIsInLiteralString(temp_str, temp_str.at(i), i))
&& (!this_char_was_already_checked)) {
if (temp_str.at(i-1) != '\n' && temp_str.at(i-2) != '\n') {
temp_str.insert(i - 1, "\n\n");
this_char_was_already_checked = true;
}
}
else if ((temp_str.at(i) == '<') && !(charIsInLiteralString(temp_str, temp_str.at(i), i))
&& (!this_char_was_already_checked) && (i > 0)) {
if (temp_str.at(i-1) != '\n') {
temp_str.insert(i - 1, "\n");
this_char_was_already_checked = true;
}
}
if (this_char_was_already_checked == true)
this_char_was_already_checked = false; //reset it to false
}
scriptStr = temp_str;
}
I didn't think of controlling the loop with scriptStr.size(), and iterating through and operating on temp_str, at the same time, before your post, so thanks.