diff --git a/libc/include/regex.h b/libc/include/regex.h index 8681a12d..dd0bfcca 100644 --- a/libc/include/regex.h +++ b/libc/include/regex.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, 2015 Jonas 'Sortie' Termansen. + * Copyright (c) 2014, 2015, 2016 Jonas 'Sortie' Termansen. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -140,6 +140,9 @@ typedef struct #define REG_NOTBOL (1 << 0) #define REG_NOTEOL (1 << 1) +#if __USE_SORTIX +#define REG_STARTEND (1 << 2) +#endif #define REG_NOMATCH 1 #define REG_BADPAT 2 @@ -154,6 +157,9 @@ typedef struct #define REG_ERANGE 11 #define REG_ESPACE 12 #define REG_BADRPT 13 +#if __USE_SORTIX +#define REG_INVARG 14 +#endif #ifdef __cplusplus extern "C" { diff --git a/libc/regex/regerror.c b/libc/regex/regerror.c index aa054193..b372537a 100644 --- a/libc/regex/regerror.c +++ b/libc/regex/regerror.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, 2015 Jonas 'Sortie' Termansen. + * Copyright (c) 2014, 2015, 2016 Jonas 'Sortie' Termansen. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -43,6 +43,7 @@ size_t regerror(int errnum, case REG_ERANGE: msg = "Invalid endpoint in range expression"; break; case REG_ESPACE: msg = "Out of memory"; break; case REG_BADRPT: msg = "'?', '*', or '+' not preceded by valid regular expression"; break; + case REG_INVARG: msg = "Invalid arguments"; break; } if ( errbuf_size ) strlcpy(errbuf, msg, errbuf_size); diff --git a/libc/regex/regexec.c b/libc/regex/regexec.c index c60b5eac..6ab125ca 100644 --- a/libc/regex/regexec.c +++ b/libc/regex/regexec.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, 2015 Jonas 'Sortie' Termansen. + * Copyright (c) 2014, 2015, 2016 Jonas 'Sortie' Termansen. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -21,6 +21,7 @@ #include #include #include +#include #define QUEUE_CURRENT_STATE(new_state) \ { \ @@ -105,6 +106,21 @@ int regexec(const regex_t* restrict regex_const, if ( regex->re_cflags & REG_NOSUB ) nmatch = 0; + size_t start; + size_t end; + if ( eflags & REG_STARTEND ) + { + if ( pmatch[0].rm_so < 0 || pmatch[0].rm_eo < pmatch[0].rm_so ) + return REG_INVARG; + start = pmatch[0].rm_so; + end = pmatch[0].rm_eo; + } + else + { + start = 0; + end = strlen(string); + } + for ( size_t i = 0; i < nmatch; i++ ) { pmatch[i].rm_so = -1; @@ -123,7 +139,7 @@ int regexec(const regex_t* restrict regex_const, regex->re->re_is_current = 0; - for ( size_t i = 0; true; i++ ) + for ( size_t i = start; i <= end; i++ ) { if ( !regex->re->re_is_current && result == REG_NOMATCH ) { @@ -143,7 +159,7 @@ int regexec(const regex_t* restrict regex_const, regex->re->re_matches[m].rm_eo = -1; } } - char c = string[i]; + char c = i < end ? string[i] : '\0'; for ( struct re* state = current_states; state; state = state->re_current_state_next ) @@ -237,9 +253,6 @@ int regexec(const regex_t* restrict regex_const, if ( current_states == NULL && result == 0 ) break; - - if ( c == '\0' ) - break; } pthread_mutex_unlock(®ex->re_lock);