>>>>> "chris" == Chris Banford <[EMAIL PROTECTED]> writes:

 chris> After a bit more examination, I see that once the Strings are
 chris> input into the database correctly, that they are being
 chris> returned with the escape characters removed. So this means I
 chris> just need an easy way to replace the below stuff in a
 chris> [possibly large] string.  -Chris

Since you don't care about escaping speed, the simplest technique is
just a straightforward 'switch' in a loop like this:

{define-proc public {escapify-for-mysql s:String}:String
    || Make a StringBuf, assume there's not much escaping needed.
    let buf:StringBuf = {StringBuf efficient-size = s.size + 10}

    {for c in s do
        {switch c
         case '\u0000' do {buf.concat |"\0"|}
         case '\u0008' do {buf.concat |"\b"|}
         case '\u001A' do {buf.concat |"\z"|}
         case '\n'     do {buf.concat |"\n"|}
         case '\r'     do {buf.concat |"\r"|}
         case '\t'     do {buf.concat |"\t"|}
         case '\'', '\"', '\\', '%', '_' do
            {buf.append '\\'}
            {buf.append c}
         else
            || No escaping needed.
            {buf.append c}
        }
    }

    {if buf == s then
        || Just return original String rather than making a new one.
        || This is a fairly common case.
        {return s}
     else
        {return {buf.to-String}}
    }
}

There are numerous ways to optimize this, but this sounds adequate for
your needs.

-Mat

*******************************************
To unsubscribe from this list, send a mail to:
mailto:[EMAIL PROTECTED]
To contact a human list administrator, send a mail to:
mailto:[EMAIL PROTECTED]
To recieve a list of other options for this list, send a mail to:
mailto:[EMAIL PROTECTED]

Reply via email to