Browse code

core: snexpr - updated to version supporting escaped chars in string values

Daniel-Constantin Mierla authored on 20/09/2022 06:43:05
Showing 1 changed files
... ...
@@ -799,6 +799,7 @@ static int snexpr_next_token(const char *s, size_t len, int *flags)
799 799
 {
800 800
 	unsigned int i = 0;
801 801
 	char b;
802
+	int bsf = 0;
802 803
 	if(len == 0) {
803 804
 		return 0;
804 805
 	}
... ...
@@ -845,7 +846,12 @@ static int snexpr_next_token(const char *s, size_t len, int *flags)
845 846
 		b = c;
846 847
 		i++;
847 848
 		c = s[i];
848
-		while(c != b && i < len) {
849
+		while(i < len && (bsf==1 || c != b)) {
850
+			if(bsf == 0 && c == '\\') {
851
+				bsf = 1;
852
+			} else {
853
+				bsf = 0;
854
+			}
849 855
 			i++;
850 856
 			c = s[i];
851 857
 		}
... ...
@@ -956,6 +962,9 @@ static struct snexpr snexpr_varref(struct snexpr_var *v)
956 962
 static struct snexpr snexpr_conststr(const char *value, int len)
957 963
 {
958 964
 	struct snexpr e = snexpr_init();
965
+	char *p;
966
+	int i;
967
+	int bsf = 0;
959 968
 	if(len < 2) {
960 969
 		len = 0;
961 970
 	} else {
... ...
@@ -966,9 +975,34 @@ static struct snexpr snexpr_conststr(const char *value, int len)
966 975
 	e.param.stz.sval = malloc(len + 1);
967 976
 	if(e.param.stz.sval) {
968 977
 		if(len > 0) {
969
-			/* do not copy the quotes */
970
-			memcpy(e.param.stz.sval, value + 1, len);
971
-			e.param.stz.sval[len] = '\0';
978
+			/* do not copy the quotes - start from value[1] */
979
+			p = e.param.stz.sval;
980
+			for(i=0; i<len; i++) {
981
+				if(bsf==0 && value[i+1]=='\\') {
982
+					bsf = 1;
983
+				} else if(bsf==1) {
984
+					bsf = 0;
985
+					switch(value[i+1]) {
986
+						case 'n':
987
+							*p = '\n';
988
+						break;
989
+						case 'r':
990
+							*p = '\r';
991
+						break;
992
+						case 't':
993
+							*p = '\t';
994
+						break;
995
+						default:
996
+							*p = value[i+1];
997
+					}
998
+					p++;
999
+				} else {
1000
+					bsf = 0;
1001
+					*p = value[i+1];
1002
+					p++;
1003
+				}
1004
+			}
1005
+			*p = '\0';
972 1006
 		} else {
973 1007
 			e.param.stz.sval[0] = '\0';
974 1008
 		}