These are a set of functions that provide basic text operations such as Find, Compare and
Find & Replace. Also included here is an Insert Breaks function, and a Constrain String Size function.
-- Text Services API
-- (c) 2000 Michael Nieves
-- TSAPI_Find(search_string, content_string, case_sensitive, start)
-- returns index of match, or 0 if no match
-- case_sensitive is an optional boolean
-- start is optional char index, defaults to 1
-- TSAPI_Compare(string1, string2, case_sensitive)
-- returns 1 if string1 and string2 match, 0 if no match
-- case_sensitive is an optional boolean
-- TSAPI_Replace(search_string, replacement_string, content_string, case_sensitive, boundary)
-- returns string with replacement, or original string if no match
-- boundary can optionally be set to 'ALL' to replace all occurences of search_string
-- case_sensitive is an optional boolean
-- TSAPI_InsertBreaks(str, maxCharPerLine)
-- returns string with breaks inserted between words
-- maxCharPerLine specifies the maxlength of a line including spaces before a break is inserted
-- TSAPI_ConstrainStringSize(str, maxLength)
-- returns string truncated to max length, appends elipses
on TSAPI_Find search_string, content_string, case_sensitive, start
if start = EMPTY OR start = VOID then
set start to 1
end if
repeat with i = start to length(content_string)
if case_sensitive = TRUE then
if charToNum(char 1 of search_string) = charToNum(char i of content_string) then
set count to i
repeat with k = 1 to length(search_string)
if charToNum(char k of search_string) = charToNum(char count of content_string) then
set count to count + 1
else
exit repeat
end if
end repeat
end if
else
if char 1 of search_string = char i of content_string then
set count to i
repeat with k = 1 to length(search_string)
if char k of search_string = char count of content_string then
set count to count + 1
else
exit repeat
end if
end repeat
end if
end if
if k > length(search_string) then
return i
end if
end repeat
return 0
end TSAPI_Find
on TSAPI_Compare string1, string2, case_sensitive
if length(string1) <> length(string2) then
return 0
end if
if case_sensitive = TRUE then
repeat with k = 1 to length(string1)
if charToNum(char k of string1) <> charToNum(char k of string2) then
return 0
end if
end repeat
return 1
else
repeat with k = 1 to length(string1)
if char k of string1 <> char k of string2 then
return 0
end if
end repeat
return 1
end if
return 0
end TSAPI_Compare
on TSAPI_Replace search_string, replacement_string, content_string, case_sensitive, boundary
set index to TSAPI_Find(search_string, content_string, case_sensitive)
if index = 0 then
return content_string
end if
repeat with i = 1 to length(content_string)
if i = index then
set new_string to new_string & replacement_string
set start to i + (length(search_string))
if boundary = "ALL" then
set index to TSAPI_Find(search_string, content_string, case_sensitive, start)
end if
else if i >= start then
set new_string to new_string & char i of content_string
end if
end repeat
return new_string
end TSAPI_Replace
on TSAPI_InsertBreaks(str, maxCharPerLine)
set the itemDelimiter to SPACE
new_str = EMPTY
repeat with i = 1 to (the number of items in str)
if new_str <> EMPTY then
temp_str = new_str & SPACE & (item i of str)
if length(temp_str) > maxCharPerLine then
temp_str = new_str & RETURN & (item i of str)
maxCharPerLine = maxCharPerLine * 2
end if
new_str = temp_str
else
new_str = (item i of str)
end if
end repeat
return new_str
end
on TSAPI_ConstrainStringSize(str, maxLength)
if length(str) > maxLength then
str = chars(str, 1, (maxLength - 6)) & "... "
else
repeat with k = length(str) to maxLength - 1
str = str & SPACE
end repeat
end if
return str
end
Contact
MMI
36 South Court Sq
Suite 300
Newnan, GA 30263
USA