Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commitb04065e59bed8334794bc657e83e1dd89ef077ae (patch)
Author
 Syndamia <kamen@syndamia.com>
2023-10-19 12:51:23 +0300
Committer
 Syndamia <kamen@syndamia.com>
2023-10-19 12:51:23 +0300
Files63aefac3e2d651139ce8972097665966b20ff92b
uni-projects-b04065e59bed8334794bc657e83e1dd89ef077ae.tar
uni-projects-b04065e59bed8334794bc657e83e1dd89ef077ae.tar.gz
uni-projects-b04065e59bed8334794bc657e83e1dd89ef077ae.zip
Parent54df57e7cc0dd58f56a8657cfb68e91164483038 (diff)
[CI/PA1] Reworked escapeString to be more robust and simpler
1 files changed, 18 insertions, 15 deletions
-rw-r--r--Year 3/Compilers, Introduction/coolc-spim/my-code/coursework/pa1/cool.flex33
diff --git a/Year 3/Compilers, Introduction/coolc-spim/my-code/coursework/pa1/cool.flex b/Year 3/Compilers, Introduction/coolc-spim/my-code/coursework/pa1/cool.flex
index 8a16b94..6db6354 100644
--- a/Year 3/Compilers, Introduction/coolc-spim/my-code/coursework/pa1/cool.flex
+++ b/Year 3/Compilers, Introduction/coolc-spim/my-code/coursework/pa1/cool.flex
@@ -38,26 +38,29 @@ extern YYSTYPE cool_yylval;
* Add Your own definitions here
*/
-char* escapeString(char* str) {
- unsigned escapes = 0, length = 0;
- for (char* i = str; *i != 0; i++) {
- if (*i == '\\') escapes++;
- else if (*i == '\n') curr_lineno++;
+char* escapeString(const char* str) {
+ unsigned length = 0;
+ for (const char* i = str; *i != '\0'; ++i) {
+ if (*i == '\\') ++i;
+ if (*i == '\n') curr_lineno++; // this maybe shouldn't be here, but it's fine
length++;
}
- char* out = new char[length -= 2];
+
+ char* out = new char[length - 1];
+ out[length - 2] = '\0';
char* currout = out;
- bool skipnext = false;
- while (length > 0 && *(++str + 1) != 0) {
- if (*str != '\\') {
- *(currout++) = *str;
+
+ for (const char* i = str + 1; i[1] != '\0'; ++i) {
+ if (*i != '\\') *(currout++) = *i;
+ else switch (i[1]) {
+ case 'b': *(currout++) = '\b'; ++i; break;
+ case 't': *(currout++) = '\t'; ++i; break;
+ case 'n': *(currout++) = '\n'; ++i; break;
+ case 'f': *(currout++) = '\f'; ++i; break;
+ case '\\': *(currout++) = '\\'; ++i; break;
}
- else if (str[1] == 'b') { *(currout++) = '\b'; str++; }
- else if (str[1] == 't') { *(currout++) = '\t'; str++; }
- else if (str[1] == 'n') { *(currout++) = '\n'; str++; }
- else if (str[1] == 'f') { *(currout++) = '\f'; str++; }
- else if (str[1] == '\\') { *(currout++) = '\\'; str++; }
}
+
return out;
}