Always pass the script string around by reference

This commit is contained in:
2019-06-13 17:37:23 +02:00
parent 10a2535c96
commit 601c4a3f89
7 changed files with 16 additions and 28 deletions

View File

@@ -5,15 +5,9 @@
#include "Lexer.hpp"
Lexer::Lexer(string* scriptString, class Script* script) {
this -> _scriptString = scriptString;
this->_scriptSize = scriptString->size();
this -> ScriptData = script;
this -> _position = 0;
}
Lexer::Lexer(const string& scriptString, class Script *script) {
this -> _scriptString = new string(scriptString);
Lexer::Lexer(const string& scriptString, class Script* script)
: _scriptString(scriptString)
{
this->_scriptSize = scriptString.size();
this -> ScriptData = script;
this -> _position = 0;
@@ -38,7 +32,7 @@ vector<IToken*> Lexer::Lex() {
char Lexer::Peek(){
if (Lexer::_position >= this -> _scriptSize)
return '\0';
return this -> _scriptString->at(Lexer::_position);
return this -> _scriptString.at(Lexer::_position);
}
char Lexer::Next(){
@@ -200,7 +194,7 @@ IToken * Lexer::LexIdentifierOrKeyword() {
}
}
string s = this -> _scriptString->substr(start, end - start + 1);
string s = this -> _scriptString.substr(start, end - start + 1);
switch (HashedString::ConstHash(s.c_str())){
case HashedString::ConstHash("and"): return new SimpleToken(TokenKind::AndKeyword, start, 3);
case HashedString::ConstHash("break"): return new SimpleToken(TokenKind::BreakKeyword, start, 5);
@@ -258,7 +252,7 @@ IToken* Lexer::LexString(char c){
return new SimpleToken(TokenKind::BadToken, start, end -start + 1);
}
string s = this -> _scriptString->substr(start + 1, end - start);
string s = this -> _scriptString.substr(start + 1, end - start);
stringstream stream;
for (int i = 0; i < s.size(); i++){
c = s[i];

View File

@@ -8,7 +8,7 @@
using namespace std;
class Lexer {
string* _scriptString;
const string& _scriptString;
#ifdef TESTS_BUILD
public:
#endif
@@ -24,7 +24,6 @@ public:
Script* ScriptData;
vector<IToken*> Lex();
explicit Lexer(string* scriptString, class Script* script);
explicit Lexer(const string& scriptString, class Script* script);
};