This is the mail archive of the libc-hacker@sources.redhat.com mailing list for the glibc project.

Note that libc-hacker is a closed list. You may look at the archives of this list, but subscription and posting are not open.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Regex infrastructure for UTF-8 optimizations


Hi!

This is just easy MB_CUR_MAX -> something->mb_cur_max replacement,
needed by following patches.  At the same time it reshuffles re_dfa_t a
little so that it is smaller on 64-bit arches.

2003-11-12  Jakub Jelinek  <jakub@redhat.com>

	* posix/regex_internal.h (struct re_string_t): Add is_utf8
	and mb_cur_max fields.
	(struct re_dfa_t): Likewise.  Reorder fields to make structure
	smaller on 64-bit arches.
	(re_string_allocate, re_string_construct): Add mb_cur_max and
	is_utf8 arguments.
	(re_string_char_size_at, re_string_wchar_at): Use pstr->mb_cur_max
	instead of MB_CUR_MAX.
	* posix/regcomp.c (re_compile_fastmap_iter): Use dfa->mb_cur_max
	instead of MB_CUR_MAX.
	(re_compile_internal): Pass new arguments to re_string_construct.
	(init_dfa): Initialize mb_cur_max and is_utf8 fields.
	(peek_token, peek_token_bracket): Use input->mb_cur_max instead
	of MB_CUR_MAX.
	(parse_expression, parse_bracket_exp, parse_charclass_op): Use
	dfa->mb_cur_max instead of MB_CUR_MAX.
	* posix/regex_internal.c (re_string_construct_common): Add
	mb_cur_max and is_utf8 arguments.  Initialize fields with them.
	(re_string_allocate, re_string_construct): Add mb_cur_max and
	is_utf8 arguments, pass them to re_string_construct_common.
	Use mb_cur_max instead of MB_CUR_MAX.
	(re_string_realloc_buffers): Use pstr->mb_cur_max instead of
	MB_CUR_MAX.
	(re_string_reconstruct): Likewise.
	(re_string_context_at): Use input->mb_cur_max instead of
	MB_CUR_MAX.
	(create_ci_newstate, create_cd_newstate): Use dfa->mb_cur_max
	instead of MB_CUR_MAX.
	* posix/regexec.c (re_search_internal): Likewise.
	Pass new arguments to re_string_allocate.
	(check_matching, transit_state_sb): Use dfa->mb_cur_max instead of
	MB_CUR_MAX.
	(extend_buffers): Use pstr->mb_cur_max instead of MB_CUR_MAX.

--- libc/posix/regcomp.c.jj	2003-11-12 08:25:35.000000000 +0100
+++ libc/posix/regcomp.c	2003-11-12 09:26:47.000000000 +0100
@@ -312,7 +312,7 @@ re_compile_fastmap_iter (bufp, init_stat
 {
   re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
   int node_cnt;
-  int icase = (MB_CUR_MAX == 1 && (bufp->syntax & RE_ICASE));
+  int icase = (dfa->mb_cur_max == 1 && (bufp->syntax & RE_ICASE));
   for (node_cnt = 0; node_cnt < init_state->nodes.nelem; ++node_cnt)
     {
       int node = init_state->nodes.elems[node_cnt];
@@ -324,7 +324,7 @@ re_compile_fastmap_iter (bufp, init_stat
 #ifdef RE_ENABLE_I18N
 	  if ((bufp->syntax & RE_ICASE) && !icase)
 	    {
-	      unsigned char *buf = alloca (MB_CUR_MAX), *p;
+	      unsigned char *buf = alloca (dfa->mb_cur_max), *p;
 	      wchar_t wc;
 	      mbstate_t state;
 
@@ -376,7 +376,7 @@ re_compile_fastmap_iter (bufp, init_stat
 			re_set_fastmap (fastmap, icase, ch);
 		}
 # else
-	      if (MB_CUR_MAX > 1)
+	      if (dfa->mb_cur_max > 1)
 		for (i = 0; i < SBC_MAX; ++i)
 		  if (__btowc (i) == WEOF)
 		    re_set_fastmap (fastmap, icase, i);
@@ -744,7 +744,8 @@ re_compile_internal (preg, pattern, leng
 #endif
 
   err = re_string_construct (&regexp, pattern, length, preg->translate,
-			     syntax & RE_ICASE);
+			     syntax & RE_ICASE, dfa->mb_cur_max,
+			     dfa->is_utf8);
   if (BE (err != REG_NOERROR, 0))
     {
       re_free (dfa);
@@ -812,6 +813,13 @@ init_dfa (dfa, pat_len)
   dfa->subexps = re_malloc (re_subexp_t, dfa->subexps_alloc);
   dfa->word_char = NULL;
 
+  dfa->mb_cur_max = MB_CUR_MAX;
+#ifdef _LIBC
+  if (dfa->mb_cur_max > 1
+      && strcmp (_NL_CURRENT (LC_CTYPE, _NL_CTYPE_CODESET_NAME), "UTF-8") == 0)
+    dfa->is_utf8 = 1;
+#endif
+
   if (BE (dfa->nodes == NULL || dfa->state_table == NULL
 	  || dfa->subexps == NULL, 0))
     {
@@ -1522,7 +1530,7 @@ peek_token (token, input, syntax)
 
 #ifdef RE_ENABLE_I18N
   token->mb_partial = 0;
-  if (MB_CUR_MAX > 1 &&
+  if (input->mb_cur_max > 1 &&
       !re_string_first_byte (input, re_string_cur_idx (input)))
     {
       token->type = CHARACTER;
@@ -1738,7 +1746,7 @@ peek_token_bracket (token, input, syntax
   token->opr.c = c;
 
 #ifdef RE_ENABLE_I18N
-  if (MB_CUR_MAX > 1 &&
+  if (input->mb_cur_max > 1 &&
       !re_string_first_byte (input, re_string_cur_idx (input)))
     {
       token->type = CHARACTER;
@@ -1976,7 +1984,7 @@ parse_expression (regexp, preg, token, s
 	  return NULL;
 	}
 #ifdef RE_ENABLE_I18N
-      if (MB_CUR_MAX > 1)
+      if (dfa->mb_cur_max > 1)
 	{
 	  while (!re_string_eoi (regexp)
 		 && !re_string_first_byte (regexp, re_string_cur_idx (regexp)))
@@ -2111,7 +2119,7 @@ parse_expression (regexp, preg, token, s
 	  *err = REG_ESPACE;
 	  return NULL;
 	}
-      if (MB_CUR_MAX > 1)
+      if (dfa->mb_cur_max > 1)
 	dfa->has_mb_node = 1;
       break;
     case OP_WORD:
@@ -2883,7 +2891,7 @@ parse_bracket_exp (regexp, dfa, token, s
 	  goto parse_bracket_exp_free_return;
 	}
 #ifdef RE_ENABLE_I18N
-      if (MB_CUR_MAX > 1)
+      if (dfa->mb_cur_max > 1)
 	for (i = 0; i < SBC_MAX; ++i)
 	  if (__btowc (i) == WEOF)
 	    bitset_set (sbcset, i);
@@ -3042,8 +3050,8 @@ parse_bracket_exp (regexp, dfa, token, s
 
 #ifdef RE_ENABLE_I18N
   if (mbcset->nmbchars || mbcset->ncoll_syms || mbcset->nequiv_classes
-      || mbcset->nranges || (MB_CUR_MAX > 1 && (mbcset->nchar_classes
-						|| mbcset->non_match)))
+      || mbcset->nranges || (dfa->mb_cur_max > 1 && (mbcset->nchar_classes
+						     || mbcset->non_match)))
     {
       re_token_t alt_token;
       bin_tree_t *mbc_tree;
@@ -3377,7 +3385,7 @@ build_charclass_op (dfa, trans, class_na
 	bitset_set(cset->sbcset, '\0');
       */
       mbcset->non_match = 1;
-      if (MB_CUR_MAX > 1)
+      if (dfa->mb_cur_max > 1)
 	for (i = 0; i < SBC_MAX; ++i)
 	  if (__btowc (i) == WEOF)
 	    bitset_set (sbcset, i);
@@ -3423,7 +3431,7 @@ build_charclass_op (dfa, trans, class_na
     goto build_word_op_espace;
 
 #ifdef RE_ENABLE_I18N
-  if (MB_CUR_MAX > 1)
+  if (dfa->mb_cur_max > 1)
     {
       re_token_t alt_token;
       bin_tree_t *mbc_tree;
--- libc/posix/regex_internal.h.jj	2003-11-11 12:19:25.000000000 +0100
+++ libc/posix/regex_internal.h	2003-11-12 09:47:13.000000000 +0100
@@ -334,6 +334,8 @@ struct re_string_t
   RE_TRANSLATE_TYPE trans;
   /* 1 if REG_ICASE.  */
   unsigned int icase : 1;
+  unsigned int is_utf8 : 1;
+  int mb_cur_max;
 };
 typedef struct re_string_t re_string_t;
 /* In case of REG_ICASE, we allocate the buffer dynamically for mbs.  */
@@ -345,10 +347,12 @@ typedef struct re_string_t re_string_t;
 
 static reg_errcode_t re_string_allocate (re_string_t *pstr, const char *str,
 					 int len, int init_len,
-					 RE_TRANSLATE_TYPE trans, int icase);
+					 RE_TRANSLATE_TYPE trans, int icase,
+					 int mb_cur_max, int is_utf8);
 static reg_errcode_t re_string_construct (re_string_t *pstr, const char *str,
 					  int len, RE_TRANSLATE_TYPE trans,
-					  int icase);
+					  int icase, int mb_cur_max,
+					  int is_utf8);
 static reg_errcode_t re_string_reconstruct (re_string_t *pstr, int idx,
 					    int eflags, int newline);
 static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr,
@@ -571,11 +575,7 @@ struct re_fail_stack_t
 struct re_dfa_t
 {
   re_bitset_ptr_t word_char;
-
-  /* number of subexpressions `re_nsub' is in regex_t.  */
-  int subexps_alloc;
   re_subexp_t *subexps;
-
   re_token_t *nodes;
   int nodes_alloc;
   int nodes_len;
@@ -586,11 +586,14 @@ struct re_dfa_t
   re_node_set *eclosures;
   re_node_set *inveclosures;
   struct re_state_table_entry *state_table;
-  unsigned int state_hash_mask;
   re_dfastate_t *init_state;
   re_dfastate_t *init_state_word;
   re_dfastate_t *init_state_nl;
   re_dfastate_t *init_state_begbuf;
+
+  /* number of subexpressions `re_nsub' is in regex_t.  */
+  int subexps_alloc;
+  unsigned int state_hash_mask;
   int states_alloc;
   int init_node;
   int nbackref; /* The number of backreference in this dfa.  */
@@ -604,6 +607,8 @@ struct re_dfa_t
      a node which can accept multibyte character or multi character
      collating element.  */
   unsigned int has_mb_node : 1;
+  unsigned int is_utf8 : 1;
+  int mb_cur_max;
 };
 typedef struct re_dfa_t re_dfa_t;
 
@@ -700,7 +705,7 @@ re_string_char_size_at (pstr, idx)
      int idx;
 {
   int byte_idx;
-  if (MB_CUR_MAX == 1)
+  if (pstr->mb_cur_max == 1)
     return 1;
   for (byte_idx = 1; idx + byte_idx < pstr->valid_len; ++byte_idx)
     if (pstr->wcs[idx + byte_idx] != WEOF)
@@ -713,7 +718,7 @@ re_string_wchar_at (pstr, idx)
      const re_string_t *pstr;
      int idx;
 {
-  if (MB_CUR_MAX == 1)
+  if (pstr->mb_cur_max == 1)
     return (wint_t) pstr->mbs[idx];
   return (wint_t) pstr->wcs[idx];
 }
--- libc/posix/regex_internal.c.jj	2003-11-12 08:26:45.000000000 +0100
+++ libc/posix/regex_internal.c	2003-11-12 09:48:11.000000000 +0100
@@ -20,7 +20,8 @@
 
 static void re_string_construct_common (const char *str, int len,
 					re_string_t *pstr,
-					RE_TRANSLATE_TYPE trans, int icase);
+					RE_TRANSLATE_TYPE trans, int icase,
+					int mb_cur_max, int is_utf8);
 #ifdef RE_ENABLE_I18N
 static int re_string_skip_chars (re_string_t *pstr, int new_raw_idx,
 				 wint_t *last_wc);
@@ -46,15 +47,17 @@ static unsigned int inline calc_state_ha
    re_string_reconstruct before using the object.  */
 
 static reg_errcode_t
-re_string_allocate (pstr, str, len, init_len, trans, icase)
+re_string_allocate (pstr, str, len, init_len, trans, icase,
+		    mb_cur_max, is_utf8)
      re_string_t *pstr;
      const char *str;
-     int len, init_len, icase;
+     int len, init_len, icase, mb_cur_max, is_utf8;
      RE_TRANSLATE_TYPE trans;
 {
   reg_errcode_t ret;
   int init_buf_len = (len + 1 < init_len) ? len + 1: init_len;
-  re_string_construct_common (str, len, pstr, trans, icase);
+  re_string_construct_common (str, len, pstr, trans, icase,
+			      mb_cur_max, is_utf8);
   pstr->stop = pstr->len;
 
   ret = re_string_realloc_buffers (pstr, init_buf_len);
@@ -65,21 +68,22 @@ re_string_allocate (pstr, str, len, init
 		    : (unsigned char *) str);
   pstr->mbs = MBS_ALLOCATED (pstr) ? pstr->mbs : pstr->mbs_case;
   pstr->valid_len = (MBS_CASE_ALLOCATED (pstr) || MBS_ALLOCATED (pstr)
-		     || MB_CUR_MAX > 1) ? pstr->valid_len : len;
+		     || mb_cur_max > 1) ? pstr->valid_len : len;
   return REG_NOERROR;
 }
 
 /* This function allocate the buffers, and initialize them.  */
 
 static reg_errcode_t
-re_string_construct (pstr, str, len, trans, icase)
+re_string_construct (pstr, str, len, trans, icase, mb_cur_max, is_utf8)
      re_string_t *pstr;
      const char *str;
-     int len, icase;
+     int len, icase, mb_cur_max, is_utf8;
      RE_TRANSLATE_TYPE trans;
 {
   reg_errcode_t ret;
-  re_string_construct_common (str, len, pstr, trans, icase);
+  re_string_construct_common (str, len, pstr, trans, icase,
+			      mb_cur_max, is_utf8);
   pstr->stop = pstr->len;
   /* Set 0 so that this function can initialize whole buffers.  */
   pstr->valid_len = 0;
@@ -97,7 +101,7 @@ re_string_construct (pstr, str, len, tra
   if (icase)
     {
 #ifdef RE_ENABLE_I18N
-      if (MB_CUR_MAX > 1)
+      if (mb_cur_max > 1)
 	build_wcs_upper_buffer (pstr);
       else
 #endif /* RE_ENABLE_I18N  */
@@ -106,7 +110,7 @@ re_string_construct (pstr, str, len, tra
   else
     {
 #ifdef RE_ENABLE_I18N
-      if (MB_CUR_MAX > 1)
+      if (mb_cur_max > 1)
 	build_wcs_buffer (pstr);
       else
 #endif /* RE_ENABLE_I18N  */
@@ -131,7 +135,7 @@ re_string_realloc_buffers (pstr, new_buf
      int new_buf_len;
 {
 #ifdef RE_ENABLE_I18N
-  if (MB_CUR_MAX > 1)
+  if (pstr->mb_cur_max > 1)
     {
       wint_t *new_array = re_realloc (pstr->wcs, wint_t, new_buf_len);
       if (BE (new_array == NULL, 0))
@@ -163,18 +167,20 @@ re_string_realloc_buffers (pstr, new_buf
 
 
 static void
-re_string_construct_common (str, len, pstr, trans, icase)
+re_string_construct_common (str, len, pstr, trans, icase, mb_cur_max, is_utf8)
      const char *str;
      int len;
      re_string_t *pstr;
      RE_TRANSLATE_TYPE trans;
-     int icase;
+     int icase, mb_cur_max, is_utf8;
 {
   memset (pstr, '\0', sizeof (re_string_t));
   pstr->raw_mbs = (const unsigned char *) str;
   pstr->len = len;
   pstr->trans = trans;
   pstr->icase = icase ? 1 : 0;
+  pstr->mb_cur_max = mb_cur_max;
+  pstr->is_utf8 = is_utf8;
 }
 
 #ifdef RE_ENABLE_I18N
@@ -372,7 +378,7 @@ re_string_translate_buffer (pstr)
 }
 
 /* This function re-construct the buffers.
-   Concretely, convert to wide character in case of MB_CUR_MAX > 1,
+   Concretely, convert to wide character in case of pstr->mb_cur_max > 1,
    convert to upper case in case of REG_ICASE, apply translation.  */
 
 static reg_errcode_t
@@ -385,7 +391,7 @@ re_string_reconstruct (pstr, idx, eflags
     {
       /* Reset buffer.  */
 #ifdef RE_ENABLE_I18N
-      if (MB_CUR_MAX > 1)
+      if (pstr->mb_cur_max > 1)
 	memset (&pstr->cur_state, '\0', sizeof (mbstate_t));
 #endif /* RE_ENABLE_I18N */
       pstr->len += pstr->raw_mbs_idx;
@@ -409,7 +415,7 @@ re_string_reconstruct (pstr, idx, eflags
 	  pstr->tip_context = re_string_context_at (pstr, offset - 1, eflags,
 						    newline);
 #ifdef RE_ENABLE_I18N
-	  if (MB_CUR_MAX > 1)
+	  if (pstr->mb_cur_max > 1)
 	    memmove (pstr->wcs, pstr->wcs + offset,
 		     (pstr->valid_len - offset) * sizeof (wint_t));
 #endif /* RE_ENABLE_I18N */
@@ -429,7 +435,7 @@ re_string_reconstruct (pstr, idx, eflags
 	  /* No, skip all characters until IDX.  */
 	  pstr->valid_len = 0;
 #ifdef RE_ENABLE_I18N
-	  if (MB_CUR_MAX > 1)
+	  if (pstr->mb_cur_max > 1)
 	    {
 	      int wcs_idx;
 	      wint_t wc;
@@ -467,7 +473,7 @@ re_string_reconstruct (pstr, idx, eflags
 
   /* Then build the buffers.  */
 #ifdef RE_ENABLE_I18N
-  if (MB_CUR_MAX > 1)
+  if (pstr->mb_cur_max > 1)
     {
       if (pstr->icase)
 	build_wcs_upper_buffer (pstr);
@@ -519,7 +525,7 @@ re_string_context_at (input, idx, eflags
 		: CONTEXT_NEWLINE | CONTEXT_ENDBUF);
     }
 #ifdef RE_ENABLE_I18N
-  if (MB_CUR_MAX > 1)
+  if (input->mb_cur_max > 1)
     {
       wint_t wc;
       int wc_idx = idx;
@@ -1157,7 +1163,7 @@ create_ci_newstate (dfa, nodes, hash)
 	newstate->halt = 1;
 #ifdef RE_ENABLE_I18N
       else if (type == COMPLEX_BRACKET
-	       || (type == OP_PERIOD && MB_CUR_MAX > 1))
+	       || (type == OP_PERIOD && dfa->mb_cur_max > 1))
 	newstate->accept_mb = 1;
 #endif /* RE_ENABLE_I18N */
       else if (type == OP_BACK_REF)
@@ -1208,7 +1214,7 @@ create_cd_newstate (dfa, nodes, context,
 	newstate->halt = 1;
 #ifdef RE_ENABLE_I18N
       else if (type == COMPLEX_BRACKET
-	       || (type == OP_PERIOD && MB_CUR_MAX > 1))
+	       || (type == OP_PERIOD && dfa->mb_cur_max > 1))
 	newstate->accept_mb = 1;
 #endif /* RE_ENABLE_I18N */
       else if (type == OP_BACK_REF)
--- libc/posix/regexec.c.jj	2003-10-07 11:55:50.000000000 +0200
+++ libc/posix/regexec.c	2003-11-12 09:22:45.000000000 +0100
@@ -605,7 +605,8 @@ re_search_internal (preg, string, length
   fl_longest_match = (nmatch != 0 || dfa->nbackref);
 
   err = re_string_allocate (&input, string, length, dfa->nodes_len + 1,
-			    preg->translate, preg->syntax & RE_ICASE);
+			    preg->translate, preg->syntax & RE_ICASE,
+			    dfa->mb_cur_max, dfa->is_utf8);
   if (BE (err != REG_NOERROR, 0))
     goto free_return;
   input.stop = stop;
@@ -643,7 +644,7 @@ re_search_internal (preg, string, length
   incr = (range < 0) ? -1 : 1;
   left_lim = (range < 0) ? start + range : start;
   right_lim = (range < 0) ? start : start + range;
-  sb = MB_CUR_MAX == 1;
+  sb = dfa->mb_cur_max == 1;
   fast_translate = sb || !(preg->syntax & RE_ICASE || preg->translate);
 
   for (;;)
@@ -1018,7 +1019,7 @@ check_matching (preg, mctx, fl_search, f
 	      /* Restart from initial state, since we are searching
 		 the point from where matching start.  */
 #ifdef RE_ENABLE_I18N
-	      if (MB_CUR_MAX == 1
+	      if (dfa->mb_cur_max == 1
 		  || re_string_first_byte (mctx->input, cur_str_idx))
 #endif /* RE_ENABLE_I18N */
 		cur_state = acquire_init_state_context (&err, preg, mctx,
@@ -2280,7 +2281,7 @@ transit_state_sb (err, preg, state, fl_s
     {
 #ifdef RE_ENABLE_I18N
       int not_initial = 0;
-      if (MB_CUR_MAX > 1)
+      if (dfa->mb_cur_max > 1)
 	for (node_cnt = 0; node_cnt < next_nodes.nelem; ++node_cnt)
 	  if (dfa->nodes[next_nodes.elems[node_cnt]].type == CHARACTER)
 	    {
@@ -3772,7 +3773,7 @@ extend_buffers (mctx)
   if (pstr->icase)
     {
 #ifdef RE_ENABLE_I18N
-      if (MB_CUR_MAX > 1)
+      if (pstr->mb_cur_max > 1)
 	build_wcs_upper_buffer (pstr);
       else
 #endif /* RE_ENABLE_I18N  */
@@ -3781,7 +3782,7 @@ extend_buffers (mctx)
   else
     {
 #ifdef RE_ENABLE_I18N
-      if (MB_CUR_MAX > 1)
+      if (pstr->mb_cur_max > 1)
 	build_wcs_buffer (pstr);
       else
 #endif /* RE_ENABLE_I18N  */

	Jakub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]