Subject | Re: [firebird-support] UDF - another way? |
---|---|
Author | Ivan Prenosil |
Post date | 2004-12-27T14:19Z |
> From: "Nick Upson"This does not implement all the functionality of your UDF,
> I'd rather implement this as an SP than a UDF, can anyone see a efficient way
> to do so?
but now it should be easy for you to modify this skeleton
so it does what you need ...
Ivan
http://www.volny.cz/iprenosil/interbase/
CREATE OR ALTER PROCEDURE titlecase (s VARCHAR(100))
RETURNS (result VARCHAR(100)) AS
DECLARE VARIABLE c CHAR(1); -- current character
DECLARE VARIABLE p CHAR(1); -- previous character
BEGIN
/* Skip leading blanks */
WHILE (s LIKE ' %') DO
s = SUBSTRING(s FROM 2);
p = ' ';
result = '';
WHILE (s <> ' ') DO BEGIN
c = SUBSTRING(s FROM 1 FOR 1);
IF (p IN (' ', '-', '''')) THEN c = UPPER(c);
result = result || c;
p = c;
s = SUBSTRING(s FROM 2);
END
SUSPEND;
END
----- Original Message -----
From: "Nick Upson" <nick@...>
Sent: Tuesday, December 21, 2004 4:17 PM
Subject: [firebird-support] UDF - another way?
>
> I'd rather implement this as an SP than a UDF, can anyone see a efficient way
> to do so?
>
> char* EXPORT fn_titlecase(char* s)
> {
> char *buffer = (char *)malloc(256);
> short j = 0;
> char *pos1;
>
> while (*s == ' ') /* skip leading blanks */
> s++;
>
> *s = toupper(*s); /* uppercase first char */
>
> while (*s) /* copy the rest */
> {
> buffer[j++] = *s++;
> }
>
> buffer[j] = '\0';
>
> /* convert any character that follows a space and first char */
> for (pos1 = buffer, pos1 = strstr(pos1, " "); pos1 != NULL; pos1 =
> strstr(pos1, " "))
> {
> pos1 = pos1 + 1;
> *pos1 = toupper(*pos1);
> }
>
> /* convert character following special strings */
>
> for (pos1 = buffer, pos1 = strstr(pos1, "-"); pos1 != NULL; pos1 =
> strstr(pos1, "-"))
> {
> pos1 = pos1 + 1;
> *pos1 = toupper(*pos1);
> }
> for (pos1 = buffer, pos1 = strstr(pos1, "'"); pos1 != NULL; pos1 =
> strstr(pos1, "'"))
> {
> pos1 = pos1 + 1;
> if ( (pos1 - 3) < buffer || *(pos1 - 3) == ' ') /* only if
> preceeded by space, letter */
> *pos1 = toupper(*pos1);
> }
> for (pos1 = buffer, pos1 = strstr(pos1, "("); pos1 != NULL; pos1 =
> strstr(pos1, "("))
> {
> pos1 = pos1 + 1;
> *pos1 = toupper(*pos1);
> }
> for (pos1 = buffer, pos1 = strstr(pos1, "Mc"); pos1 != NULL; pos1 =
> strstr(pos1, "Mc"))
> {
> pos1 = pos1 + 2;
> *pos1 = toupper(*pos1);
> }
>
> buffer [strlen(s)-1] = '\0';
> return buffer;
> }