[REBOL] Re: Custom Capitalize Function
From: carl:cybercraft at: 25-May-2002 11:13
On 25-May-02, Dr. Louis A. Turk wrote:
> Hi rebols,
> I need a function to capitalize the first character after the second
> space in string-b only if the first character after the second space
> in string a is capitalized. Example:
> string-a: "Chapter 1:1 Itu besar merah rumah."
> string-b: "Chapter 1:1 the big red house."
> capitalize-it string-a string-b
> string-b: "Chapter 1:1 The big Red house."
> This function is used right in the heart of a script that processes
> a rather massive document, so speed is essential.
> Any ideas for a fast, elegant function?
capitalize-it: function [a [string!] b [string!]][found][
if not found: find/tail find/tail a " " " " [return]
if all [found/1 >= #"A" found/1 <= #"Z"] [
if not found: find/tail find/tail b " " " " [return]
if all [found/1 >= #"a" found/1 <= #"z"] [
change found to-char to-integer found/1 - 32
Seems to work...
>> a: "a b c" b: "x y z" capitalize-it a b
== "a b c"
== "x y z"
>> a: "a b C" b: "x y z" capitalize-it a b
== "a b C"
== "x y Z"
If you're sure all your strings will have two spaces in you could take
out the checks for that. ie, remove the "if not" and "[return]" from
the two lines where the strings are searched.
There's probably a faster way, but this shouldn't be too slow -