Browse Source

faux.str: Add quote closed flag to faux_str_nextword()

Serj Kalichev 3 years ago
parent
commit
605c69775a
3 changed files with 15 additions and 4 deletions
  1. 1 1
      faux/str.h
  2. 3 1
      faux/str/str.c
  3. 11 2
      faux/str/testc_str.c

+ 1 - 1
faux/str.h

@@ -39,7 +39,7 @@ char *faux_str_c_esc(const char *src);
 char *faux_str_c_bin(const char *src, size_t n);
 
 char *faux_str_nextword(const char *str, const char **saveptr,
-	const char *alt_quotes);
+	const char *alt_quotes, bool_t *qclosed);
 
 
 //const char *faux_str_suffix(const char *string);

+ 3 - 1
faux/str/str.c

@@ -622,7 +622,7 @@ static char *faux_str_deesc(const char *string, size_t len)
  * @warning Returned alocated buffer must be freed later by faux_str_free()
  */
 char *faux_str_nextword(const char *str, const char **saveptr,
-	const char *alt_quotes)
+	const char *alt_quotes, bool_t *qclosed)
 {
 	const char *string = str;
 	const char *word = NULL;
@@ -760,6 +760,8 @@ char *faux_str_nextword(const char *str, const char **saveptr,
 
 	if (saveptr)
 		*saveptr = string;
+	if (qclosed)
+		*qclosed = ! (dbl_quoted || alt_quoted);
 
 	return result;
 }

+ 11 - 2
faux/str/testc_str.c

@@ -7,9 +7,10 @@
 
 int testc_faux_str_nextword(void)
 {
-	const char* line = "asd\"\\\"\"mmm  `ll\"l\\p\\\\m```j`j`` ```kk``pp``` ll\\ l  \"aaa\"bbb`ccc```ddd``eee ``lk\\\"";
+	const char* line = "asd\"\\\"\"mmm \"``\" `ll\"l\\p\\\\m```j`j`` ```kk``pp``` ll\\ l  \"aaa\"bbb`ccc```ddd``eee ``lk\\\"";
 	const char* etalon[] = {
 		"asd\"mmm",
+		"``",
 		"ll\"l\\p\\\\mj`j",
 		"kk``pp",
 		"ll l",
@@ -20,6 +21,7 @@ int testc_faux_str_nextword(void)
 	int retval = 0;
 	int i = 0;
 	const char *saveptr = line;
+	bool_t closed_quotes = BOOL_FALSE;
 
 	printf("Line   : [%s]\n", line);
 
@@ -27,7 +29,7 @@ int testc_faux_str_nextword(void)
 		int r = -1;
 		char *res = NULL;
 		printf("Etalon %d : [%s]\n", i, etalon[i]);
-		res = faux_str_nextword(saveptr, &saveptr, "`");
+		res = faux_str_nextword(saveptr, &saveptr, "`", &closed_quotes);
 		if (!res) {
 			printf("The faux_str_nextword() return value is NULL\n");
 			break;
@@ -41,6 +43,13 @@ int testc_faux_str_nextword(void)
 		}
 		faux_str_free(res);
 	}
+	// Last quote is unclosed
+	if (closed_quotes) {
+		printf("Closed quotes flag is wrong\n");
+		retval = -1;
+	} else {
+		printf("Really unclosed quotes\n");
+	}
 
 	return retval;
 }