Skip to content

Commit

Permalink
Move reduceWhitespace from nall to bass core
Browse files Browse the repository at this point in the history
  • Loading branch information
ARM9 committed Sep 10, 2015
1 parent 72ab2d2 commit 00370e2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
27 changes: 26 additions & 1 deletion bass/core/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ bool Bass::source(const string& filename) {
for(unsigned lineNumber = 0; lineNumber < lines.size(); lineNumber++) {
if(auto position = lines[lineNumber].find("//")) lines[lineNumber].resize(position()); //remove comments

lines[lineNumber].reduceWhitespace();
reduceWhitespace(lines[lineNumber]);

lstring blocks = lines[lineNumber].qsplit(";").strip();
for(unsigned blockNumber = 0; blockNumber < blocks.size(); blockNumber++) {
Expand All @@ -60,6 +60,31 @@ bool Bass::source(const string& filename) {
return true;
}

string& Bass::reduceWhitespace(string& s) {
// '[ ]+' -> ' '
// ad-hoc whitespace compression
// this only fixes some problems, see bass/.test/space_test
bool insideQuotes = false;
char *p = s.data();

for(unsigned i = 0; i < s.length(); i++) {
if(!insideQuotes) {
if(p[i] == ' ') {
unsigned j = i+1;
for(; j < s.length() && p[j] == ' '; j++) {}
if(j > i+1) {
memmove(p + i+1, p + j, s.length() - (j - 1));
}
}
}
if(p[i] == '"') {
insideQuotes = !insideQuotes;
}
}
//s.resize(s.length());
return s;
}

void Bass::define(const string& name, const string& value) {
defines.insert({name, value});
}
Expand Down
2 changes: 2 additions & 0 deletions bass/core/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ struct Bass {
template<typename... Args> void warning(Args&&... args);
template<typename... Args> void error(Args&&... args);

string& reduceWhitespace(string& s);

//evaluate
enum class Evaluation : unsigned { Default = 0, Strict = 1 }; //strict mode disallows forward-declaration of constants
int64_t evaluate(const string& expression, Evaluation mode = Evaluation::Default);
Expand Down
2 changes: 0 additions & 2 deletions nall/string/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ struct string {
inline maybe<unsigned> qfind(rstring key) const;
inline maybe<unsigned> iqfind(rstring key) const;

inline string& reduceWhitespace();

//core.hpp
inline explicit operator bool() const;
inline operator const char*() const;
Expand Down
23 changes: 0 additions & 23 deletions nall/string/wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,29 +119,6 @@ maybe<unsigned> string::ifind(rstring key) const { return istrpos(data(), key);
maybe<unsigned> string::qfind(rstring key) const { return qstrpos(data(), key); }
maybe<unsigned> string::iqfind(rstring key) const { return iqstrpos(data(), key); }

string& string::reduceWhitespace() {
// '[ ]+' -> ' '
// ad-hoc whitespace compression
// this only fixes some problems, see bass/.test/space_test
bool insideQuotes = false;
for(unsigned i = 0; i < length(); i++) {
if(!insideQuotes){
if(data()[i] == ' ') {
unsigned j = i+1;
for(; j < length() && data()[j] == ' '; j++) {}
if(j > i+1) {
memmove(data() + i+1, data() + j, length() - (j - 1));
}
}
}
if(data()[i] == '"'){
insideQuotes = !insideQuotes;
}
}
//resize(length());
return *this;
}

}

#endif

0 comments on commit 00370e2

Please sign in to comment.