From eb010688aff5db396cc900eb99b20174a0a1f46c Mon Sep 17 00:00:00 2001 From: David Anderson Date: Wed, 1 Feb 2006 12:10:26 +0000 Subject: [PATCH] optimized + fixed parsing in strbreak() --- amxmodx/string.cpp | 99 +++++++++++++++++++++------------------------- 1 file changed, 46 insertions(+), 53 deletions(-) diff --git a/amxmodx/string.cpp b/amxmodx/string.cpp index 3edef569..e84a8237 100755 --- a/amxmodx/string.cpp +++ b/amxmodx/string.cpp @@ -678,73 +678,66 @@ static cell AMX_NATIVE_CALL amx_strtok(AMX *amx, cell *params) //strbreak(String[], First[], FirstLen, Rest[], RestLen) static cell AMX_NATIVE_CALL strbreak(AMX *amx, cell *params) /* 5 param */ { - bool quote_flag = false; - bool done_flag = false; - int left_pos = 0; - int right_pos = 0; - int l = 0; - unsigned int i = 0; - char hold = '"'; + int _len; + bool in_quote = false; + bool had_quotes = false; + size_t i = 0; + size_t beg = 0; - char *string = get_amxstring(amx, params[1], 0, l); - char *left = new char[strlen(string) + 1]; - char *right = new char[strlen(string) + 1]; + char *string = get_amxstring(amx, params[1], 0, _len); + cell *left = get_amxaddr(amx, params[2]); + cell *right = get_amxaddr(amx, params[4]); int LeftMax = params[3]; int RightMax = params[5]; - for (i = 0; i < (unsigned int)l; i++) + size_t len = (size_t)_len; + + while (isspace(string[i]) && i RightMax) ? RightMax : pos - _end; + size_t to_go = end-beg; + if (end && to_go) { - right[right_pos] = hold; + while (to_go--) + *left++ = (cell)*start++; } - - right_pos++; + *left = '\0'; + end = (len-i+1 > LeftMax) ? LeftMax : len-i+1; + if (end) + { + start = &(string[i]); + while (end--) + *right++ = (cell)*start++; + } + *right = '\0'; + return 1; } } } - left[left_pos] = '\0'; - right[right_pos] = '\0'; - set_amxstring(amx, params[2], left, params[3]); - set_amxstring(amx, params[4], right, params[5]); - delete [] left; - delete [] right; + //if we got here, there was nothing to break + set_amxstring(amx, params[2], &(string[beg]), params[3]); + if (RightMax) + *right = '\0'; return 1; }