text
stringlengths
0
15.7k
source
stringlengths
6
112
if sourceText is missing value then return 0
string.applescript
if (offset of substring in sourceText) is 0 then return 0
string.applescript
set theList to split(sourceText, substring)
string.applescript
return (length of sourceText) - (length of last item of theList) - (length of substring) + 1
string.applescript
end lastIndexOf
string.applescript
on replaceAll(sourceText, substring, replacement)
string.applescript
end replaceAll
string.applescript
on replace(sourceText, substring, replacement)
string.applescript
if substring is "
string.applescript
" then
string.applescript
set ugly to ""
string.applescript
repeat with nextChar in characters of sourceText
string.applescript
if (ASCII number nextChar) is 10 then
string.applescript
set ugly to ugly & replacement
string.applescript
set ugly to ugly & nextChar
string.applescript
return ugly
string.applescript
if sourceText is equal to substring then return replacement
string.applescript
if sourceText starts with substring then
string.applescript
set startIdx to the (length of substring) + 1
string.applescript
return replacement & replace(text startIdx thru length of sourceText, substring, replacement)
string.applescript
if sourceText is missing value then return sourceText
string.applescript
if (offset of substring in sourceText) is equal to 0 then return sourceText
string.applescript
set retval to text 1 thru ((offset of substring in sourceText) - 1) of sourceText
string.applescript
set nextIdx to offset of substring in sourceText
string.applescript
if nextIdx - 1 is greater than ((length of sourceText) - (length of substring)) then exit repeat
string.applescript
if text nextIdx thru (nextIdx + (length of substring) - 1) of sourceText is equal to substring then
string.applescript
if length of replacement is greater than 0 then
string.applescript
set retval to retval & replacement
string.applescript
set nextIdx to nextIdx + (length of substring)
string.applescript
set retval to retval & item nextIdx of sourceText
string.applescript
set nextIdx to nextIdx + 1
string.applescript
if nextIdx is less than or equal to length of sourceText then set retval to retval & text nextIdx thru -1 of sourceText
string.applescript
retval
string.applescript
on substring(thisText, startIdx, endIdx)
string.applescript
if endIdx is less than the startIdx then return missing value
string.applescript
text startIdx thru endIdx of thisText
string.applescript
end substring
string.applescript
on substringFrom(thisText, startIdx)
string.applescript
if startIdx is greater than the (count of thisText) then return thisText
string.applescript
text startIdx thru -1 of thisText
string.applescript
end substringFrom
string.applescript
on rtrim(theText)
string.applescript
set trimOffset to count of theText
string.applescript
repeat until (character trimOffset of theText) is not in {" ", " "}
string.applescript
set trimOffset to trimOffset - 1
string.applescript
if trimOffset is 0 then
string.applescript
text 1 thru trimOffset of theText
string.applescript
end rtrim
string.applescript
on ltrim(theText as text)
string.applescript
if theText is "" then return ""
string.applescript
set trimOffset to 1
string.applescript
repeat until (character trimOffset of theText) is not in {" ", " ", tab}
string.applescript
set trimOffset to trimOffset + 1
string.applescript
if trimOffset is greater than or equal to (count of theText) then
string.applescript
text trimOffset thru (count of theText) of theText
string.applescript
end ltrim
string.applescript
on trim(theText)
string.applescript
do shell script "echo \"" & theText & "\" | sed 's/ *$//g' | sed 's/^ *//g'"
string.applescript
on printAsciiNumber(theString)
string.applescript
repeat with nextChar in characters of theString
string.applescript
log nextChar & " " & (ASCII number nextChar)
string.applescript
end printAsciiNumber
string.applescript
on isUnicode(theString)
string.applescript
set inspected to ""
string.applescript
set inspected to inspected & (ASCII character (ASCII number nextChar))
string.applescript
theString is not equal to inspected
string.applescript
end isUnicode
string.applescript
on hasUnicode(theString)
string.applescript
isUnicode(theString)
string.applescript
end hasUnicode
string.applescript
on removeEnding(theText as text, ending as text)
string.applescript
text 1 thru -((length of ending) + 1) of theText
string.applescript
theText
string.applescript
end removeEnding
string.applescript
on unitTest()
string.applescript
set utLib to std's import("unit-test")
string.applescript
set ut to utLib's new()
string.applescript
tell ut
string.applescript
newMethod("ltrim")
string.applescript
assertEqual("SELECT", my ltrim("
string.applescript
SELECT"), "Multiline")
string.applescript
assertEqual("SELECT", my ltrim("SELECT"), "no leading whitespace")
string.applescript
assertEqual("", my ltrim(" "), "spaces only")
string.applescript
assertEqual("", my ltrim(""), "empty string")
string.applescript
newMethod("replaceFirst")
string.applescript
assertEqual("three two plus one", my replaceFirst("three one plus one", "one", "two"), "Happy Case")
string.applescript
assertEqual("one", my replaceFirst("one", "{}", "found"), "Not Found")
string.applescript
assertEqual("one", my replaceFirst("one", "three", "dummy"), "Substring is longer")
string.applescript
newMethod("format")
string.applescript
assertEqual("one-two", my format("{}-{}", {"one", "two"}), "Bugged")
string.applescript
assertEqual("Ends: yo", my format("Ends: {}", "yo"), "Ends with")
string.applescript
assertEqual("Cat's daily stand up", my format("{} daily stand up", "Cat's"), "With single quote")
string.applescript
assertEqual("With Bang!", my format("With {}", "Bang!"), "With bang")
string.applescript
assertEqual("{\"attr-name\": 1}", my format("{\"{}\": 1}", "attr-name"), "Double Quoted")
string.applescript
set expected to "javascript;
string.applescript
$('a') = 'hello';"
string.applescript
set actual to my format("javascript;
string.applescript
$('{}') = '{}';", {"a", "hello"})
string.applescript
assertEqual(expected, actual, "Multiline")
string.applescript
newMethod("replace")
string.applescript