From 3e6700d1576c6582169a5d50c95da106ee3017cf Mon Sep 17 00:00:00 2001 From: Fatih Kızmaz <barka_21@hotmail.com> Date: Thu, 19 May 2022 05:03:44 +0300 Subject: Added ability to escape quotes in strings. (#110) * Added ability to escape quotes in strings. * Removed type checking for customfunc arguments. It was a bad idea to check types, because php automatically tries to convert a parameter to required type and throws if it failures. On the other hand, we can check types also in callables if required. * Update phpdoc * Fix some typos + improve min, max, avg funcs. * Update readme + improvements. * Fix a typo in sample. * Fix unshown backslash in readme. --- src/NXP/Classes/Tokenizer.php | 58 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 9 deletions(-) (limited to 'src/NXP/Classes/Tokenizer.php') diff --git a/src/NXP/Classes/Tokenizer.php b/src/NXP/Classes/Tokenizer.php index c8b415d..af041c1 100644 --- a/src/NXP/Classes/Tokenizer.php +++ b/src/NXP/Classes/Tokenizer.php @@ -50,27 +50,67 @@ class Tokenizer public function tokenize() : self { - foreach (\str_split($this->input, 1) as $ch) { + $isLastCharEscape = false; + + foreach (\str_split($this->input) as $ch) { switch (true) { case $this->inSingleQuotedString: - if ("'" === $ch) { - $this->tokens[] = new Token(Token::String, $this->stringBuffer); - $this->inSingleQuotedString = false; - $this->stringBuffer = ''; + if ('\\' === $ch) { + if ($isLastCharEscape) { + $this->stringBuffer .= '\\'; + $isLastCharEscape = false; + } else { + $isLastCharEscape = true; + } + + continue 2; + } elseif ("'" === $ch) { + if ($isLastCharEscape) { + $this->stringBuffer .= "'"; + $isLastCharEscape = false; + } else { + $this->tokens[] = new Token(Token::String, $this->stringBuffer); + $this->inSingleQuotedString = false; + $this->stringBuffer = ''; + } continue 2; } + + if ($isLastCharEscape) { + $this->stringBuffer .= '\\'; + $isLastCharEscape = false; + } $this->stringBuffer .= $ch; continue 2; case $this->inDoubleQuotedString: - if ('"' === $ch) { - $this->tokens[] = new Token(Token::String, $this->stringBuffer); - $this->inDoubleQuotedString = false; - $this->stringBuffer = ''; + if ('\\' === $ch) { + if ($isLastCharEscape) { + $this->stringBuffer .= '\\'; + $isLastCharEscape = false; + } else { + $isLastCharEscape = true; + } continue 2; + } elseif ('"' === $ch) { + if ($isLastCharEscape) { + $this->stringBuffer .= '"'; + $isLastCharEscape = false; + } else { + $this->tokens[] = new Token(Token::String, $this->stringBuffer); + $this->inDoubleQuotedString = false; + $this->stringBuffer = ''; + } + + continue 2; + } + + if ($isLastCharEscape) { + $this->stringBuffer .= '\\'; + $isLastCharEscape = false; } $this->stringBuffer .= $ch; -- cgit v1.2.3