4395: Update str_utf8_stats documentation, minor refactoring r=heinrich5991 a=Robyt3

This removes the check for the null-terminator from the while loop, as this is also checked in `str_utf8_forward`, which returns an unchanged size in this case. The comparison `new_size != *size` was previously redundant for that reason.

Update comments and add some empty lines for consistency.

`@heinrich5991` 

## Checklist

- [X] Tested the change ingame
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test if it works standalone, system.c especially
- [ ] Considered possible null pointers and out of bounds array indexing
- [ ] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: Robert Müller <robert.mueller@uni-siegen.de>
This commit is contained in:
bors[bot] 2021-11-26 02:39:09 +00:00 committed by GitHub
commit 5c5469ba0a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 11 deletions

View file

@ -3458,18 +3458,15 @@ void str_utf8_stats(const char *str, int max_size, int max_count, int *size, int
{
*size = 0;
*count = 0;
while(str[*size] && *size < max_size && *count < max_count)
while(*size < max_size && *count < max_count)
{
int new_size = str_utf8_forward(str, *size);
if(new_size != *size)
{
if(new_size >= max_size || *count >= max_count)
if(new_size == *size || new_size >= max_size || *count >= max_count)
break;
*size = new_size;
++(*count);
}
}
}
unsigned str_quickhash(const char *str)
{

View file

@ -2091,7 +2091,7 @@ void str_utf8_copy(char *dst, const char *src, int dst_size);
/*
Function: str_utf8_stats
Determines the byte size and utf8 character count of a string.
Determines the byte size and utf8 character count of a utf8 string.
Parameters:
str - Pointer to the string.
@ -2101,21 +2101,24 @@ void str_utf8_copy(char *dst, const char *src, int dst_size);
count - Pointer to store count of utf8 characters of the string.
Remarks:
- Assumes nothing about the encoding of the string.
It's the users responsibility to make sure the bounds are aligned.
- The string is treated as zero-terminated utf8 string.
- It's the user's responsibility to make sure the bounds are aligned.
*/
void str_utf8_stats(const char *str, int max_size, int max_count, int *size, int *count);
/*
Function: str_next_token
Writes the next token after str into buf, returns the rest of the string.
Parameters:
str - Pointer to string.
delim - Delimiter for tokenization.
buffer - Buffer to store token in.
buffer_size - Size of the buffer.
Returns:
Pointer to rest of the string.
Remarks:
- The token is always null-terminated.
*/
@ -2287,6 +2290,7 @@ int secure_rand_below(int below);
/*
Function: set_console_msg_color
Sets the console color.
Parameters:
rgb - If NULL it will reset the console color to default, else it will transform the rgb color to a console color
*/