mirror of https://github.com/Genymobile/scrcpy
Remove confusing sc_str_truncate()
This util function was error-prone: - it accepted a buffer as parameter (not necessarily a NUL-terminated string) and its length (including the NUL char, if any); - it wrote '\0' over the last character of the buffer, so the last character was lost if the buffer was not a NUL-terminated string, and even worse, it caused undefined behavior if the length was empty; - it returned the length of the resulting NUL-terminated string, which was inconsistent with the input buffer length. In addition, it was not necessarily optimal: - it wrote '\0' twice; - it required to know the buffer length, that is the input string length + 1, in advance. Remove this function, and let the client use strcspn() manually.
This commit is contained in:
parent
6d41c53b61
commit
137d2c9791
|
@ -297,14 +297,6 @@ error:
|
|||
return NULL;
|
||||
}
|
||||
|
||||
size_t
|
||||
sc_str_truncate(char *data, size_t len, const char *endchars) {
|
||||
data[len - 1] = '\0';
|
||||
size_t idx = strcspn(data, endchars);
|
||||
data[idx] = '\0';
|
||||
return idx;
|
||||
}
|
||||
|
||||
ssize_t
|
||||
sc_str_index_of_column(const char *s, unsigned col, const char *seps) {
|
||||
size_t colidx = 0;
|
||||
|
|
|
@ -103,17 +103,6 @@ sc_str_from_wchars(const wchar_t *s);
|
|||
char *
|
||||
sc_str_wrap_lines(const char *input, unsigned columns, unsigned indent);
|
||||
|
||||
/**
|
||||
* Truncate the data after any of the characters from `endchars`
|
||||
*
|
||||
* An '\0' is always written at the end of the data string, even if no
|
||||
* character from `endchars` is encountered.
|
||||
*
|
||||
* Return the size of the resulting string (as strlen() would return).
|
||||
*/
|
||||
size_t
|
||||
sc_str_truncate(char *data, size_t len, const char *endchars);
|
||||
|
||||
/**
|
||||
* Find the start of a column in a string
|
||||
*
|
||||
|
|
|
@ -338,32 +338,6 @@ static void test_wrap_lines(void) {
|
|||
free(formatted);
|
||||
}
|
||||
|
||||
static void test_truncate(void) {
|
||||
char s[] = "hello\nworld\n!";
|
||||
size_t len = sc_str_truncate(s, sizeof(s), "\n");
|
||||
|
||||
assert(len == 5);
|
||||
assert(!strcmp("hello", s));
|
||||
|
||||
char s2[] = "hello\r\nworkd\r\n!";
|
||||
len = sc_str_truncate(s2, sizeof(s2), "\n\r");
|
||||
|
||||
assert(len == 5);
|
||||
assert(!strcmp("hello", s));
|
||||
|
||||
char s3[] = "hello world\n!";
|
||||
len = sc_str_truncate(s3, sizeof(s3), " \n\r");
|
||||
|
||||
assert(len == 5);
|
||||
assert(!strcmp("hello", s3));
|
||||
|
||||
char s4[] = "hello ";
|
||||
len = sc_str_truncate(s4, sizeof(s4), " \n\r");
|
||||
|
||||
assert(len == 5);
|
||||
assert(!strcmp("hello", s4));
|
||||
}
|
||||
|
||||
static void test_index_of_column(void) {
|
||||
assert(sc_str_index_of_column("a bc d", 0, " ") == 0);
|
||||
assert(sc_str_index_of_column("a bc d", 1, " ") == 2);
|
||||
|
@ -417,7 +391,6 @@ int main(int argc, char *argv[]) {
|
|||
test_parse_integer_with_suffix();
|
||||
test_strlist_contains();
|
||||
test_wrap_lines();
|
||||
test_truncate();
|
||||
test_index_of_column();
|
||||
test_remove_trailing_cr();
|
||||
return 0;
|
||||
|
|
Loading…
Reference in New Issue