Scrap chartorunearr(), introducing utftorunestr()
Interface and function as proposed by cls. The reasoning behind this function is that cls expressed his interest to keep memory allocation out of libutf, which is a very good motive. This simplifies the function a lot and should also increase the speed a bit, but the most important factor here is that there's no malloc anywhere in libutf, making it a lot smaller and more robust with a smaller attack-surface. Look at the paste(1) and tr(1) changes for an idiomatic way to allocate the right amount of space for the Rune-array.
This commit is contained in:
@@ -1,27 +0,0 @@
|
||||
/* See LICENSE file for copyright and license details. */
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../util.h"
|
||||
#include "../utf.h"
|
||||
|
||||
int
|
||||
chartorunearr(const char *str, Rune **r)
|
||||
{
|
||||
size_t len = strlen(str), rlen, roff, ret = 1, i;
|
||||
Rune s;
|
||||
|
||||
for (rlen = 0, roff = 0; roff < len && ret; rlen++) {
|
||||
ret = charntorune(&s, str + roff, MAX(UTFmax, len - roff));
|
||||
roff += ret;
|
||||
}
|
||||
|
||||
*r = emalloc(rlen * sizeof(Rune) + 1);
|
||||
(*r)[rlen] = 0;
|
||||
|
||||
for (i = 0, roff = 0; roff < len && i < rlen; i++) {
|
||||
roff += charntorune(&(*r)[i], str + roff, MAX(UTFmax, len - roff));
|
||||
}
|
||||
|
||||
return rlen;
|
||||
}
|
13
libutf/utftorunestr.c
Normal file
13
libutf/utftorunestr.c
Normal file
@@ -0,0 +1,13 @@
|
||||
/* See LICENSE file for copyright and license details. */
|
||||
#include "../utf.h"
|
||||
|
||||
int
|
||||
utftorunestr(const char *str, Rune *r)
|
||||
{
|
||||
int i, n;
|
||||
|
||||
for(i = 0; (n = chartorune(&r[i], str)) && r[i]; i++)
|
||||
str += n;
|
||||
|
||||
return i;
|
||||
}
|
Reference in New Issue
Block a user