Comments (2)
I stumbled across this and had a look: The problem is, that while utf8spn
/utf8cspn
are logically equivalent to strspn
/strcspn
, their results are codepoints not bytes, so they can not be simply added to char pointers. Besides that, the API of stroke_r
demands to set the first parameter to NULL
on subsequent calls, so combining this and adding another helper (didn't find something matching in utf8.h
) I came up with:
void *utf8incr(void *utf8_restrict str, size_t len) {
char* s = (char*) str;
while(*s && len--) {
size_t l = utf8codepointcalcsize(s);
while(*s && l--) ++s;
}
return s;
}
void *utf8tok_r(void *utf8_restrict str, const void *utf8_restrict sep, void **utf8_restrict ptr) {
char* s = (char*) str;
char** p = (char**) ptr;
if (!s && !(s = *p)) {
return NULL;
}
s = utf8incr(s, utf8spn(s, sep));
if (!*s) {
return *p = 0;
}
*p = utf8incr(s, utf8cspn(s, sep));
if (**p) {
*(*p)++ = 0;
} else {
*p = 0;
}
return s;
}
And as a small change to the test:
UTEST(utf8tok_r, token_walking) {
char* string = utf8dup("this|aäáé|föőf|that|");
char* ptr = NULL;
ASSERT_EQ(0, utf8ncmp(utf8tok_r(string, "|", &ptr), "this", 4));
string = NULL;
ASSERT_EQ(0, utf8ncmp(utf8tok_r(string, "|", &ptr), "aäáé", 4));
ASSERT_EQ(0, utf8ncmp(utf8tok_r(string, "|", &ptr), "föőf", 4));
ASSERT_EQ(0, utf8ncmp(utf8tok_r(string, "|", &ptr), "that", 4));
free(string);
}
from utf8.h.
@sheredom this is a pretty interesting find
from utf8.h.
Related Issues (20)
- Possibility of dual-licensing? HOT 5
- `utf8nvalid` reads out bounds HOT 2
- strn*/utf8n* functions HOT 5
- utf8upr/lwr size issues? HOT 3
- provide get codepoint visual width function HOT 1
- utf8ncpy incorrectly removes last valid codepoint HOT 7
- Add test similar to one used in issue 109
- support utf8 convert to utf16? HOT 1
- utf8rchr issue HOT 3
- utf8makevalid read out of bounds (+ other functions) HOT 1
- utf8makevalid : test to identify sequence length and possible values not sufficient HOT 1
- grapheme support HOT 1
- Not an issue HOT 1
- Allow programmer specified allocator HOT 2
- utf8valid with size HOT 1
- Support constexpr? HOT 3
- clang-format? HOT 1
- utf8ncat - size wraparound bug HOT 1
- Way of removing malloc completely HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from utf8.h.