Skip to content

Commit 4b03c55

Browse files
committed
COMMON: allow embedded octal values in strings
1 parent 5705149 commit 4b03c55

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

samples/distro-examples/tests/strings.bas

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,5 @@ cmd_str1(0)
174174
expect = [0,0.26179938779915,0.5235987755983,0.78539816339745,1.0471975511966,1.30899693899575,1.5707963267949,1.83259571459405,2.0943951023932,2.35619449019234,2.61799387799149,2.87979326579064,3.14159265358979,3.40339204138894,3.66519142918809,3.92699081698724,4.18879020478639,4.45058959258554,4.71238898038469,4.97418836818384,5.23598775598299,5.49778714378214,5.75958653158129,6.02138591938044,6.28318530717958]
175175
if expect != seq(0, 2*pi, 360/15+1) then throw "SEQ error"
176176

177+
s="Hello\033There"
178+
if (27 != asc(mid(s, 6, 1))) then throw "err"

src/common/scan.c

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3884,6 +3884,26 @@ char *comp_load(const char *file_name) {
38843884
return buf;
38853885
}
38863886

3887+
const char *format_numeric_text(const char *str, char **output) {
3888+
const char *result = str;
3889+
int value = 0;
3890+
int digits = 0;
3891+
3892+
while (isdigit(*str)) {
3893+
value = (value << 3) + (*str - '0');
3894+
digits++;
3895+
str++;
3896+
}
3897+
3898+
if (digits == 3 && value > V_JOIN_LINE && value < 256) {
3899+
**output = value;
3900+
(*output)++;
3901+
result = str;
3902+
}
3903+
3904+
return result;
3905+
}
3906+
38873907
/**
38883908
* format source-code text
38893909
*
@@ -3895,24 +3915,21 @@ char *comp_load(const char *file_name) {
38953915
* returns a newly created string
38963916
*/
38973917
char *comp_format_text(const char *source) {
3898-
const char *p;
3899-
char *ps;
39003918
int quotes = 0;
3901-
char *new_text;
3902-
int sl, last_ch = 0, i;
3903-
char *last_nonsp_ptr;
3919+
int last_ch = 0;
39043920
int adj_line_num = 0;
39053921
int multi_line_string = 0;
39063922
int curley_brace = 0;
39073923
int square_brace = 0;
3924+
int sl = strlen(source);
3925+
char *new_text = malloc(sl + 2);
3926+
char *ps = new_text;
3927+
char *last_nonsp_ptr = new_text;
3928+
const char *p = source;
39083929

3909-
sl = strlen(source);
3910-
new_text = malloc(sl + 2);
39113930
memset(new_text, 0, sl + 2);
3912-
39133931
comp_line = 0;
3914-
p = source;
3915-
last_nonsp_ptr = ps = new_text;
3932+
39163933
while (*p) {
39173934
if (!quotes) {
39183935
switch (*p) {
@@ -3934,7 +3951,7 @@ char *comp_format_text(const char *source) {
39343951
ps++;
39353952
p++;
39363953
} else {
3937-
for (i = 0; i <= adj_line_num; i++) {
3954+
for (int i = 0; i <= adj_line_num; i++) {
39383955
// at least one nl
39393956
*ps++ = '\n';
39403957
}
@@ -4095,6 +4112,9 @@ char *comp_format_text(const char *source) {
40954112
}
40964113
// new line auto-ends the quoted string
40974114
quotes = !quotes;
4115+
} else if (*p == '\\') {
4116+
p = format_numeric_text(p + 1, &ps);
4117+
continue;
40984118
}
40994119
*ps++ = *p++;
41004120
}

0 commit comments

Comments
 (0)