diff options
author | Aaron Ball <nullspoon@oper.io> | 2021-07-11 15:31:00 -0600 |
---|---|---|
committer | Aaron Ball <nullspoon@oper.io> | 2021-07-11 15:31:00 -0600 |
commit | 2daf79139f24e78de63c37d143e40d6341146afe (patch) | |
tree | 29ee3d47ca53f7281356e30a59e2c82bd5be4162 | |
parent | a58cf9898424e773968533828441c24eec9d4e7a (diff) | |
download | gitaccess-2daf79139f24e78de63c37d143e40d6341146afe.tar.gz gitaccess-2daf79139f24e78de63c37d143e40d6341146afe.tar.xz |
Improve unwrap function
The previously used for loop is easily replaced by the strchr function
provided by string.h.
-rw-r--r-- | src/main.c | 21 |
1 files changed, 10 insertions, 11 deletions
@@ -68,10 +68,9 @@ void logmsg(char* msg) { /** * unwrap: - * Poorly written function to grab the text between two delimiters (usually - * quotes). Will traverse the provided string until the first occurence of the - * delimiter is found, and will continue until the closing delimiter is found - * (or end of line). + * Grabs the text between a delimiter (usually a quote). Will traverse the + * provided string until the first occurence of the delimiter is found, and + * will continue until the closing delimiter is found (or end of line). * * @wrapchar Delimiter character wrapping the string * @line Line to search for the wrapped string @@ -80,14 +79,14 @@ void logmsg(char* msg) { * @return Pointer to the buffer */ char* unwrap(char wrapchar, char* line, char* buf) { - int i = 0; - while(line[0] != wrapchar && line[0] != '\0') - line++; - strcpy(buf, line + 1); + char *start = NULL, *end = NULL; - while(buf[i] != wrapchar && buf[i] != '\0') - i++; - buf[i] = '\0'; + start = strchr(line, wrapchar); // Locate first occurrence + start++; // Advance one past it + end = strchr(start, wrapchar); // Locate last occurrence + + strncpy(buf, start, end - start); // Copy to the buffer + buf[end - start] = '\0'; // Null terminate return buf; } |