Add util function to remove trailing '\r'
Depending on the platform and adb versions, the lines output by adb could end with "\r\r\n". This util function helps to remove all trailing '\r'. PR #2827 <https://github.com/Genymobile/scrcpy/pull/2827>
This commit is contained in:
@@ -330,3 +330,14 @@ sc_str_index_of_column(const char *s, unsigned col, const char *seps) {
|
||||
|
||||
return col == colidx ? (ssize_t) idx : -1;
|
||||
}
|
||||
|
||||
size_t
|
||||
sc_str_remove_trailing_cr(char *s, size_t len) {
|
||||
while (len) {
|
||||
if (s[len - 1] != '\r') {
|
||||
break;
|
||||
}
|
||||
s[--len] = '\0';
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
@@ -134,4 +134,15 @@ sc_str_truncate(char *data, size_t len, const char *endchars);
|
||||
ssize_t
|
||||
sc_str_index_of_column(const char *s, unsigned col, const char *seps);
|
||||
|
||||
/**
|
||||
* Remove all `\r` at the end of the line
|
||||
*
|
||||
* The line length is provided by `len` (this avoids a call to `strlen()` when
|
||||
* the caller already knows the length).
|
||||
*
|
||||
* Return the new length.
|
||||
*/
|
||||
size_t
|
||||
sc_str_remove_trailing_cr(char *s, size_t len);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user