Search Postgresql Archives

Culturally aware initcap

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,

I've devised the following function, that performs the same task as
initcap, but in a "culturally aware fashion", for English. It solves a
common problem I was having with initcap, where the string "ROSEMARY'S
baby DOESN'T LIVE HERE anymore" became "Rosemary'S Baby doesn'T Live
Here Anymore", whereas I wanted to see "Rosemary's Baby Doesn't Live
Here Anymore", while preserving Irish names like O'Shaughnessy and
O'Sullivan. This may be more useful for exclusively English language
databases than the generic initcap().


CREATE OR REPLACE FUNCTION cul_initcap(input_val text) RETURNS text AS
$function_body$

	SELECT replace(
			replace(
				replace(
				regexp_replace(initcap($1),
						$$'([MST])([^[:upper:][:lower:]]|$)$$,
						$$'{@*#!\1!#*@}\2$$,
						'g'
						)
				, '{@*#!M!#*@}', 'm')
			, '{@*#!S!#*@}', 's')
		, '{@*#!T!#*@}', 't');
	
$function_body$
LANGUAGE 'sql' IMMUTABLE;

Now, this works, but is a little inelegant; I couldn't figure out a
better way of having regex_replace's replacement become lower case,
than wrapping part of its output in magical braces of {@*#! and !#*@}
and subsequently replacing those magical braces and their contents
with appropriate, lower-case strings using multiple replace() calls.
One obvious problem with this function is that it will not correctly
initcap a "magical brace enclosed literal", like '{@*#!T!#*@}' ,
although I dare say that isn't enough of a problem to discourage its
use.

Can someone suggest a better implementation, that doesn't rely on
magical braces? Either way, I'm going to post this on the postgres
wiki under "snippets", because I think it's of general interest, and
it currently lacks a template solution, which it probably should have.

Regards,
Peter Geoghegan

-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux