shithub: tinygl

Download patch

ref: 74d6d0ceef0cbbb19b443bce7ab5c447864a106e
parent: d1d392a4c7d2a6a81a541cdac79321c60f4c82d3
author: gek169 <gek169>
date: Thu May 13 09:46:40 EDT 2021

Automatic commit.

--- a/include-demo/stringutil.h
+++ b/include-demo/stringutil.h
@@ -478,4 +478,77 @@
 	return result;
 }
 
+static strll* parse_matched(strll* current_node, const char* tl, const char* tr){
+	strll* current_child; strll* child_old; strll* right_old;
+	long current_tl_location; long counter = 1;
+	long current_tr_location; long start_tl_location;
+	long len_tl;
+	long len_tr;
+	len_tl = strlen(tl);
+	len_tr = strlen(tr);
+	current_tl_location = strfind(current_node->text, tl);
+	start_tl_location = current_tl_location;
+	current_tr_location = strfind(current_node->text, tr);
+	if(current_tr_location > -1 && 
+		current_tr_location < current_tl_location){
+		printf("\n<SYNTAX ERROR> %s before %s\n", tr, tl);
+		exit(1);
+	}
+	if(current_tr_location == -1 &&
+		current_tl_location != -1){
+		printf("\n<SYNTAX ERROR> You have a %s, but no matching %s ?", tl, tr);
+		exit(1);
+	}
+	/*We could not find a bracketed pair!*/
+	if(current_tl_location == -1){
+		return current_node;
+	}
+	current_child = current_node->child;
+	child_old = current_node->child;
+	current_node->child = STRUTIL_CALLOC(1, sizeof(strll));
+	current_node->child->right = child_old;
+	right_old = current_node->right;
+	current_node->right = STRUTIL_CALLOC(1, sizeof(strll));
+	current_node->right->right = right_old;
+	/*Inch along, incrementing on*/
+	{ char* begin;long off = 0;
+		char* metaproc = current_node->text + current_tl_location + len_tl;
+		begin = metaproc;
+		current_tl_location = strfind(metaproc, tl);
+		current_tr_location = strfind(metaproc, tr);
+		while(counter > 0){
+			current_tl_location = strfind(metaproc, tl);
+			current_tr_location = strfind(metaproc, tr);
+			/*Handle the erroneous case- we cannot find tr.*/
+			if(current_tr_location == -1){
+				printf("\n<SYNTAX ERROR> Unmatched %s,%s group.\n", tl, tr);
+				exit(1);
+			}
+			if(current_tl_location != -1 &&
+			current_tl_location < current_tr_location){
+				off += current_tl_location + len_tl;
+				metaproc += current_tl_location + len_tl; counter++;
+			} else {
+				metaproc += current_tr_location + len_tr; counter--;
+				off += current_tr_location + len_tr;
+			}
+		}
+		if(counter < 0){
+			printf("\n<INTERNAL ERROR> Counter for %s,%s group somehow went negative.\n", tl, tr);
+			exit(1);
+		}
+		current_node->child->text = str_null_terminated_alloc(begin, off - len_tr);
+		{
+			char* text_old = current_node->text;
+			current_node->text = str_null_terminated_alloc(text_old, start_tl_location);
+			
+
+			/*Grab the post-text.*/
+			current_node->right->text = strcatalloc(begin + off, "");
+			free(text_old);
+		}
+	}
+	return current_node->child;
+}
+
 #endif