diff -cpr HEAD/src/backend/utils/adt/like.c DropMBMatchTextIC/src/backend/utils/adt/like.c
*** HEAD/src/backend/utils/adt/like.c	Thu Mar  1 09:40:18 2007
--- DropMBMatchTextIC/src/backend/utils/adt/like.c	Mon Apr  9 11:22:26 2007
*************** static int	MatchBytea(char *t, int tlen,
*** 34,48 ****
  static text *do_like_escape(text *, text *);
  
  static int	MBMatchText(char *t, int tlen, char *p, int plen);
- static int	MBMatchTextIC(char *t, int tlen, char *p, int plen);
  static text *MB_do_like_escape(text *, text *);
  
  /*--------------------
   * Support routine for MatchText. Compares given multibyte streams
   * as wide characters. If they match, returns 1 otherwise returns 0.
   *--------------------
   */
! static int
  wchareq(char *p1, char *p2)
  {
  	int			p1_len;
--- 34,50 ----
  static text *do_like_escape(text *, text *);
  
  static int	MBMatchText(char *t, int tlen, char *p, int plen);
  static text *MB_do_like_escape(text *, text *);
  
+ static int	GenericMatchText(char *s, int slen, char* p, int plen);
+ static int	mbtexticlike(text *str, text *pat);
+ 
  /*--------------------
   * Support routine for MatchText. Compares given multibyte streams
   * as wide characters. If they match, returns 1 otherwise returns 0.
   *--------------------
   */
! static __inline__ int
  wchareq(char *p1, char *p2)
  {
  	int			p1_len;
*************** wchareq(char *p1, char *p2)
*** 72,86 ****
   * of getting a single character transformed to the system's wchar_t format.
   * So now, we just downcase the strings using lower() and apply regular LIKE
   * comparison.	This should be revisited when we install better locale support.
-  *
-  * Note that MBMatchText and MBMatchTextIC do exactly the same thing now.
-  * Is it worth refactoring to avoid duplicated code?  They might become
-  * different again in the future.
   */
  
  /* Set up to compile like_match.c for multibyte characters */
  #define CHAREQ(p1, p2) wchareq(p1, p2)
- #define ICHAREQ(p1, p2) wchareq(p1, p2)
  #define NextChar(p, plen) \
  	do { int __l = pg_mblen(p); (p) +=__l; (plen) -=__l; } while (0)
  #define CopyAdvChar(dst, src, srclen) \
--- 74,86 ----
   * of getting a single character transformed to the system's wchar_t format.
   * So now, we just downcase the strings using lower() and apply regular LIKE
   * comparison.	This should be revisited when we install better locale support.
   */
  
+ #define NextByte(p, plen)	((p)++, (plen)--)
+ #define BYTEEQ(p1, p2)		(*(p1) == *(p2))
+ 
  /* Set up to compile like_match.c for multibyte characters */
  #define CHAREQ(p1, p2) wchareq(p1, p2)
  #define NextChar(p, plen) \
  	do { int __l = pg_mblen(p); (p) +=__l; (plen) -=__l; } while (0)
  #define CopyAdvChar(dst, src, srclen) \
*************** wchareq(char *p1, char *p2)
*** 91,120 ****
  	   } while (0)
  
  #define MatchText	MBMatchText
- #define MatchTextIC MBMatchTextIC
  #define do_like_escape	MB_do_like_escape
  
  #include "like_match.c"
  
- #undef CHAREQ
- #undef ICHAREQ
- #undef NextChar
- #undef CopyAdvChar
- #undef MatchText
- #undef MatchTextIC
- #undef do_like_escape
- 
  /* Set up to compile like_match.c for single-byte characters */
! #define CHAREQ(p1, p2) (*(p1) == *(p2))
  #define ICHAREQ(p1, p2) (tolower((unsigned char) *(p1)) == tolower((unsigned char) *(p2)))
! #define NextChar(p, plen) ((p)++, (plen)--)
  #define CopyAdvChar(dst, src, srclen) (*(dst)++ = *(src)++, (srclen)--)
  
  #include "like_match.c"
  
  /* And some support for BYTEA */
! #define BYTEA_CHAREQ(p1, p2) (*(p1) == *(p2))
! #define BYTEA_NextChar(p, plen) ((p)++, (plen)--)
  #define BYTEA_CopyAdvChar(dst, src, srclen) (*(dst)++ = *(src)++, (srclen)--)
  
  
--- 91,225 ----
  	   } while (0)
  
  #define MatchText	MBMatchText
  #define do_like_escape	MB_do_like_escape
  
  #include "like_match.c"
  
  /* Set up to compile like_match.c for single-byte characters */
! #define CHAREQ(p1, p2) BYTEEQ(p1, p2)
  #define ICHAREQ(p1, p2) (tolower((unsigned char) *(p1)) == tolower((unsigned char) *(p2)))
! #define NextChar(p, plen) NextByte(p, plen)
  #define CopyAdvChar(dst, src, srclen) (*(dst)++ = *(src)++, (srclen)--)
  
+ /*
+  * Same as MatchText, but ignore case
+  */
+ static int
+ MatchTextIC(char *t, int tlen, char *p, int plen)
+ {
+ 	/* Fast path for match-everything pattern */
+ 	if ((plen == 1) && (*p == '%'))
+ 		return LIKE_TRUE;
+ 
+ 	while ((tlen > 0) && (plen > 0))
+ 	{
+ 		if (*p == '\\')
+ 		{
+ 			/* Next pattern char must match literally, whatever it is */
+ 			NextByte(p, plen);
+ 			if ((plen <= 0) || !ICHAREQ(t, p))
+ 				return LIKE_FALSE;
+ 		}
+ 		else if (*p == '%')
+ 		{
+ 			/* %% is the same as % according to the SQL standard */
+ 			/* Advance past all %'s */
+ 			while ((plen > 0) && (*p == '%'))
+ 				NextByte(p, plen);
+ 			/* Trailing percent matches everything. */
+ 			if (plen <= 0)
+ 				return LIKE_TRUE;
+ 
+ 			/*
+ 			 * Otherwise, scan for a text position at which we can match the
+ 			 * rest of the pattern.
+ 			 */
+ 			while (tlen > 0)
+ 			{
+ 				/*
+ 				 * Optimization to prevent most recursion: don't recurse
+ 				 * unless first pattern char might match this text char.
+ 				 */
+ 				if (ICHAREQ(t, p) || (*p == '\\') || (*p == '_'))
+ 				{
+ 					int			matched = MatchTextIC(t, tlen, p, plen);
+ 
+ 					if (matched != LIKE_FALSE)
+ 						return matched; /* TRUE or ABORT */
+ 				}
+ 
+ 				NextChar(t, tlen);
+ 			}
+ 
+ 			/*
+ 			 * End of text with no match, so no point in trying later places
+ 			 * to start matching this pattern.
+ 			 */
+ 			return LIKE_ABORT;
+ 		}
+ 		else if ((*p != '_') && !ICHAREQ(t, p))
+ 		{
+ 			/*
+ 			 * Not the single-character wildcard and no explicit match? Then
+ 			 * time to quit...
+ 			 */
+ 			return LIKE_FALSE;
+ 		}
+ 
+ 		NextChar(t, tlen);
+ 		NextChar(p, plen);
+ 	}
+ 
+ 	if (tlen > 0)
+ 		return LIKE_FALSE;		/* end of pattern, but not of text */
+ 
+ 	/* End of input string.  Do we have matching pattern remaining? */
+ 	while ((plen > 0) && (*p == '%'))	/* allow multiple %'s at end of
+ 										 * pattern */
+ 		NextByte(p, plen);
+ 	if (plen <= 0)
+ 		return LIKE_TRUE;
+ 
+ 	/*
+ 	 * End of text with no match, so no point in trying later places to start
+ 	 * matching this pattern.
+ 	 */
+ 	return LIKE_ABORT;
+ }
+ 
  #include "like_match.c"
  
+ static __inline__ int
+ GenericMatchText(char *s, int slen, char* p, int plen)
+ {
+ 	if (pg_database_encoding_max_length() == 1)
+ 		return MatchText(s, slen, p, plen);
+ 	else
+ 		return MBMatchText(s, slen, p, plen);
+ }
+ 
+ static __inline__ int
+ mbtexticlike(text *str, text *pat)
+ {
+ 	char	   *s,
+ 			   *p;
+ 	int			slen,
+ 				plen;
+ 
+ 	/* Force inputs to lower case to achieve case insensitivity */
+ 	str = DatumGetTextP(DirectFunctionCall1(lower, PointerGetDatum(str)));
+ 	pat = DatumGetTextP(DirectFunctionCall1(lower, PointerGetDatum(pat)));
+ 	s = VARDATA(str);
+ 	slen = (VARSIZE(str) - VARHDRSZ);
+ 	p = VARDATA(pat);
+ 	plen = (VARSIZE(pat) - VARHDRSZ);
+ 
+ 	return MBMatchText(s, slen, p, plen);
+ }
+ 
  /* And some support for BYTEA */
! #define BYTEA_CHAREQ(p1, p2) BYTEEQ(p1, p2)
! #define BYTEA_NextChar(p, plen) NextByte(p, plen)
  #define BYTEA_CopyAdvChar(dst, src, srclen) (*(dst)++ = *(src)++, (srclen)--)
  
  
*************** namelike(PG_FUNCTION_ARGS)
*** 138,147 ****
  	p = VARDATA(pat);
  	plen = (VARSIZE(pat) - VARHDRSZ);
  
! 	if (pg_database_encoding_max_length() == 1)
! 		result = (MatchText(s, slen, p, plen) == LIKE_TRUE);
! 	else
! 		result = (MBMatchText(s, slen, p, plen) == LIKE_TRUE);
  
  	PG_RETURN_BOOL(result);
  }
--- 243,249 ----
  	p = VARDATA(pat);
  	plen = (VARSIZE(pat) - VARHDRSZ);
  
! 	result = (GenericMatchText(s, slen, p, plen) == LIKE_TRUE);
  
  	PG_RETURN_BOOL(result);
  }
*************** namenlike(PG_FUNCTION_ARGS)
*** 162,171 ****
  	p = VARDATA(pat);
  	plen = (VARSIZE(pat) - VARHDRSZ);
  
! 	if (pg_database_encoding_max_length() == 1)
! 		result = (MatchText(s, slen, p, plen) != LIKE_TRUE);
! 	else
! 		result = (MBMatchText(s, slen, p, plen) != LIKE_TRUE);
  
  	PG_RETURN_BOOL(result);
  }
--- 264,270 ----
  	p = VARDATA(pat);
  	plen = (VARSIZE(pat) - VARHDRSZ);
  
! 	result = (GenericMatchText(s, slen, p, plen) != LIKE_TRUE);
  
  	PG_RETURN_BOOL(result);
  }
*************** textlike(PG_FUNCTION_ARGS)
*** 186,195 ****
  	p = VARDATA(pat);
  	plen = (VARSIZE(pat) - VARHDRSZ);
  
! 	if (pg_database_encoding_max_length() == 1)
! 		result = (MatchText(s, slen, p, plen) == LIKE_TRUE);
! 	else
! 		result = (MBMatchText(s, slen, p, plen) == LIKE_TRUE);
  
  	PG_RETURN_BOOL(result);
  }
--- 285,291 ----
  	p = VARDATA(pat);
  	plen = (VARSIZE(pat) - VARHDRSZ);
  
! 	result = (GenericMatchText(s, slen, p, plen) == LIKE_TRUE);
  
  	PG_RETURN_BOOL(result);
  }
*************** textnlike(PG_FUNCTION_ARGS)
*** 210,219 ****
  	p = VARDATA(pat);
  	plen = (VARSIZE(pat) - VARHDRSZ);
  
! 	if (pg_database_encoding_max_length() == 1)
! 		result = (MatchText(s, slen, p, plen) != LIKE_TRUE);
! 	else
! 		result = (MBMatchText(s, slen, p, plen) != LIKE_TRUE);
  
  	PG_RETURN_BOOL(result);
  }
--- 306,312 ----
  	p = VARDATA(pat);
  	plen = (VARSIZE(pat) - VARHDRSZ);
  
! 	result = (GenericMatchText(s, slen, p, plen) != LIKE_TRUE);
  
  	PG_RETURN_BOOL(result);
  }
*************** nameiclike(PG_FUNCTION_ARGS)
*** 285,305 ****
  	}
  	else
  	{
- 		/* Force inputs to lower case to achieve case insensitivity */
  		text	   *strtext;
  
  		strtext = DatumGetTextP(DirectFunctionCall1(name_text,
  													NameGetDatum(str)));
! 		strtext = DatumGetTextP(DirectFunctionCall1(lower,
! 												  PointerGetDatum(strtext)));
! 		pat = DatumGetTextP(DirectFunctionCall1(lower,
! 												PointerGetDatum(pat)));
! 
! 		s = VARDATA(strtext);
! 		slen = (VARSIZE(strtext) - VARHDRSZ);
! 		p = VARDATA(pat);
! 		plen = (VARSIZE(pat) - VARHDRSZ);
! 		result = (MBMatchTextIC(s, slen, p, plen) == LIKE_TRUE);
  	}
  
  	PG_RETURN_BOOL(result);
--- 378,388 ----
  	}
  	else
  	{
  		text	   *strtext;
  
  		strtext = DatumGetTextP(DirectFunctionCall1(name_text,
  													NameGetDatum(str)));
! 		result = (mbtexticlike(strtext, pat) == LIKE_TRUE);
  	}
  
  	PG_RETURN_BOOL(result);
*************** nameicnlike(PG_FUNCTION_ARGS)
*** 326,346 ****
  	}
  	else
  	{
- 		/* Force inputs to lower case to achieve case insensitivity */
  		text	   *strtext;
  
  		strtext = DatumGetTextP(DirectFunctionCall1(name_text,
  													NameGetDatum(str)));
! 		strtext = DatumGetTextP(DirectFunctionCall1(lower,
! 												  PointerGetDatum(strtext)));
! 		pat = DatumGetTextP(DirectFunctionCall1(lower,
! 												PointerGetDatum(pat)));
! 
! 		s = VARDATA(strtext);
! 		slen = (VARSIZE(strtext) - VARHDRSZ);
! 		p = VARDATA(pat);
! 		plen = (VARSIZE(pat) - VARHDRSZ);
! 		result = (MBMatchTextIC(s, slen, p, plen) != LIKE_TRUE);
  	}
  
  	PG_RETURN_BOOL(result);
--- 409,419 ----
  	}
  	else
  	{
  		text	   *strtext;
  
  		strtext = DatumGetTextP(DirectFunctionCall1(name_text,
  													NameGetDatum(str)));
! 		result = (mbtexticlike(strtext, pat) != LIKE_TRUE);
  	}
  
  	PG_RETURN_BOOL(result);
*************** texticlike(PG_FUNCTION_ARGS)
*** 366,383 ****
  		result = (MatchTextIC(s, slen, p, plen) == LIKE_TRUE);
  	}
  	else
! 	{
! 		/* Force inputs to lower case to achieve case insensitivity */
! 		str = DatumGetTextP(DirectFunctionCall1(lower,
! 												PointerGetDatum(str)));
! 		pat = DatumGetTextP(DirectFunctionCall1(lower,
! 												PointerGetDatum(pat)));
! 		s = VARDATA(str);
! 		slen = (VARSIZE(str) - VARHDRSZ);
! 		p = VARDATA(pat);
! 		plen = (VARSIZE(pat) - VARHDRSZ);
! 		result = (MBMatchTextIC(s, slen, p, plen) == LIKE_TRUE);
! 	}
  
  	PG_RETURN_BOOL(result);
  }
--- 439,445 ----
  		result = (MatchTextIC(s, slen, p, plen) == LIKE_TRUE);
  	}
  	else
! 		result = (mbtexticlike(str, pat) == LIKE_TRUE);
  
  	PG_RETURN_BOOL(result);
  }
*************** texticnlike(PG_FUNCTION_ARGS)
*** 402,419 ****
  		result = (MatchTextIC(s, slen, p, plen) != LIKE_TRUE);
  	}
  	else
! 	{
! 		/* Force inputs to lower case to achieve case insensitivity */
! 		str = DatumGetTextP(DirectFunctionCall1(lower,
! 												PointerGetDatum(str)));
! 		pat = DatumGetTextP(DirectFunctionCall1(lower,
! 												PointerGetDatum(pat)));
! 		s = VARDATA(str);
! 		slen = (VARSIZE(str) - VARHDRSZ);
! 		p = VARDATA(pat);
! 		plen = (VARSIZE(pat) - VARHDRSZ);
! 		result = (MBMatchTextIC(s, slen, p, plen) != LIKE_TRUE);
! 	}
  
  	PG_RETURN_BOOL(result);
  }
--- 464,470 ----
  		result = (MatchTextIC(s, slen, p, plen) != LIKE_TRUE);
  	}
  	else
! 		result = (mbtexticlike(str, pat) != LIKE_TRUE);
  
  	PG_RETURN_BOOL(result);
  }
diff -cpr HEAD/src/backend/utils/adt/like_match.c DropMBMatchTextIC/src/backend/utils/adt/like_match.c
*** HEAD/src/backend/utils/adt/like_match.c	Thu Mar  1 09:40:18 2007
--- DropMBMatchTextIC/src/backend/utils/adt/like_match.c	Mon Apr  9 11:22:26 2007
***************
*** 9,19 ****
   * Before the inclusion, we need to define following macros:
   *
   * CHAREQ
-  * ICHAREQ
   * NextChar
   * CopyAdvChar
   * MatchText (MBMatchText)
-  * MatchTextIC (MBMatchTextIC)
   * do_like_escape (MB_do_like_escape)
   *
   * Copyright (c) 1996-2007, PostgreSQL Global Development Group
--- 9,17 ----
*************** MatchText(char *t, int tlen, char *p, in
*** 82,88 ****
  		if (*p == '\\')
  		{
  			/* Next pattern char must match literally, whatever it is */
! 			NextChar(p, plen);
  			if ((plen <= 0) || !CHAREQ(t, p))
  				return LIKE_FALSE;
  		}
--- 80,86 ----
  		if (*p == '\\')
  		{
  			/* Next pattern char must match literally, whatever it is */
! 			NextByte(p, plen);
  			if ((plen <= 0) || !CHAREQ(t, p))
  				return LIKE_FALSE;
  		}
*************** MatchText(char *t, int tlen, char *p, in
*** 91,97 ****
  			/* %% is the same as % according to the SQL standard */
  			/* Advance past all %'s */
  			while ((plen > 0) && (*p == '%'))
! 				NextChar(p, plen);
  			/* Trailing percent matches everything. */
  			if (plen <= 0)
  				return LIKE_TRUE;
--- 89,95 ----
  			/* %% is the same as % according to the SQL standard */
  			/* Advance past all %'s */
  			while ((plen > 0) && (*p == '%'))
! 				NextByte(p, plen);
  			/* Trailing percent matches everything. */
  			if (plen <= 0)
  				return LIKE_TRUE;
*************** MatchText(char *t, int tlen, char *p, in
*** 142,148 ****
  	/* End of input string.  Do we have matching pattern remaining? */
  	while ((plen > 0) && (*p == '%'))	/* allow multiple %'s at end of
  										 * pattern */
! 		NextChar(p, plen);
  	if (plen <= 0)
  		return LIKE_TRUE;
  
--- 140,146 ----
  	/* End of input string.  Do we have matching pattern remaining? */
  	while ((plen > 0) && (*p == '%'))	/* allow multiple %'s at end of
  										 * pattern */
! 		NextByte(p, plen);
  	if (plen <= 0)
  		return LIKE_TRUE;
  
*************** MatchText(char *t, int tlen, char *p, in
*** 154,245 ****
  }	/* MatchText() */
  
  /*
-  * Same as above, but ignore case
-  */
- static int
- MatchTextIC(char *t, int tlen, char *p, int plen)
- {
- 	/* Fast path for match-everything pattern */
- 	if ((plen == 1) && (*p == '%'))
- 		return LIKE_TRUE;
- 
- 	while ((tlen > 0) && (plen > 0))
- 	{
- 		if (*p == '\\')
- 		{
- 			/* Next pattern char must match literally, whatever it is */
- 			NextChar(p, plen);
- 			if ((plen <= 0) || !ICHAREQ(t, p))
- 				return LIKE_FALSE;
- 		}
- 		else if (*p == '%')
- 		{
- 			/* %% is the same as % according to the SQL standard */
- 			/* Advance past all %'s */
- 			while ((plen > 0) && (*p == '%'))
- 				NextChar(p, plen);
- 			/* Trailing percent matches everything. */
- 			if (plen <= 0)
- 				return LIKE_TRUE;
- 
- 			/*
- 			 * Otherwise, scan for a text position at which we can match the
- 			 * rest of the pattern.
- 			 */
- 			while (tlen > 0)
- 			{
- 				/*
- 				 * Optimization to prevent most recursion: don't recurse
- 				 * unless first pattern char might match this text char.
- 				 */
- 				if (ICHAREQ(t, p) || (*p == '\\') || (*p == '_'))
- 				{
- 					int			matched = MatchTextIC(t, tlen, p, plen);
- 
- 					if (matched != LIKE_FALSE)
- 						return matched; /* TRUE or ABORT */
- 				}
- 
- 				NextChar(t, tlen);
- 			}
- 
- 			/*
- 			 * End of text with no match, so no point in trying later places
- 			 * to start matching this pattern.
- 			 */
- 			return LIKE_ABORT;
- 		}
- 		else if ((*p != '_') && !ICHAREQ(t, p))
- 		{
- 			/*
- 			 * Not the single-character wildcard and no explicit match? Then
- 			 * time to quit...
- 			 */
- 			return LIKE_FALSE;
- 		}
- 
- 		NextChar(t, tlen);
- 		NextChar(p, plen);
- 	}
- 
- 	if (tlen > 0)
- 		return LIKE_FALSE;		/* end of pattern, but not of text */
- 
- 	/* End of input string.  Do we have matching pattern remaining? */
- 	while ((plen > 0) && (*p == '%'))	/* allow multiple %'s at end of
- 										 * pattern */
- 		NextChar(p, plen);
- 	if (plen <= 0)
- 		return LIKE_TRUE;
- 
- 	/*
- 	 * End of text with no match, so no point in trying later places to start
- 	 * matching this pattern.
- 	 */
- 	return LIKE_ABORT;
- }	/* MatchTextIC() */
- 
- /*
   * like_escape() --- given a pattern and an ESCAPE string,
   * convert the pattern to use Postgres' standard backslash escape convention.
   */
--- 152,157 ----
*************** do_like_escape(text *pat, text *esc)
*** 336,338 ****
--- 248,256 ----
  
  	return result;
  }
+ 
+ #undef CHAREQ
+ #undef NextChar
+ #undef CopyAdvChar
+ #undef MatchText
+ #undef do_like_escape
