Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api for byte length limited string #1

Open
phaag opened this issue Sep 11, 2022 · 1 comment
Open

api for byte length limited string #1

phaag opened this issue Sep 11, 2022 · 1 comment

Comments

@phaag
Copy link

phaag commented Sep 11, 2022

Hi,
Thank for this nice regex project. I am looking for a regex which allows to limit the search string by a length parameter because the search string may contain valid \0 bytes. Therefore matches after these \0 bytes would be requested.
Is there an easy way to add an api for length limited strings which may contain \0 bytes?
Many thanks

@tylov
Copy link
Owner

tylov commented Sep 12, 2022

Hi,
Yes, you can do it by

cregmatch_t match[10];
match[0].str = input; 
match[0].size = input_length;
// match[0] is both in and out param:
int ret = cregex_find(&re, input, 10, match, int creg_startend);

My STC library includes a newer version of cregex with some improvements and replace-functionality. However, it depends on a few other files in the library, see documentation. With the STC updated version, it is even simpler:

const char* data = "long  input string ....";
csview match[4];
cregex re = cregex_from("....", 0);
csview input = {.str = &data[5], .size=6};
int ret = cregex_find_sv(&re, input, match);

EDIT: actually, I haven't tested this, the regex engine may be testing both for '\0' and length internally, so it may not work. Neither C or C++ supports strings with 0 bytes inside. You should replace it with some other char / utf8 codepoint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants