text
stringlengths
0
15.7k
source
stringlengths
6
112
else if matchFormat is pattern matching then
lns3.scpt
return _findPattern(theText, forText, includeNonMatches, includeMatches)
lns3.scpt
else if matchFormat is current considerations then
lns3.scpt
if forText is "" then _support's throwInvalidParameter(forText, "for", text, "Only contains characters ignored by the current considerations.") -- checks if all characters in forText are ignored by current considering/ignoring settings (the alternative would be to return each character as a non-match separated by a zero-length match, but that's probably not what the user intended); note that unlike `aString's length = 0`, which is what library code normally uses to check for empty text, on this occasion we do want to take into account the current considering/ignoring settings so deliberately use `forText is ""` here. For example, when ignoring punctuation, searching for the TID `"!?"` is no different to searching for `""`, because all of its characters are being ignored when comparing the text being searched against the text being searched for. Thus, a simple `forText is ""` test can be used to check in advance if the text contains any matchable characters under the current considering/ignoring settings, and report a meaningful error if not.
lns3.scpt
_support's throwInvalidConstantParameter(matchFormat, "using")
lns3.scpt
else -- replace matches
lns3.scpt
if theText's length = 0 then return ""
lns3.scpt
return _replaceText(theText, forText, newText)
lns3.scpt
return _replacePattern(theText, forText, newText)
lns3.scpt
if forText is "" then _support's throwInvalidParameter(forText, "for", text, "Only contains characters ignored by the current considerations.")
lns3.scpt
_error("search text", eText, eNumber, eFrom, eTo)
lns3.scpt
end search text
lns3.scpt
to escape pattern theText
lns3.scpt
return (current application's NSRegularExpression's escapedPatternForString:(_support's asTextParameter(theText, ""))) as text
lns3.scpt
_error("escape pattern", eText, eNumber, eFrom, eTo)
lns3.scpt
end escape pattern
lns3.scpt
to escape template theText
lns3.scpt
return _joinText(_splitText(_support's asTextParameter(theText, ""), "\\"), "\\\\")
lns3.scpt
_error("escape template", eText, eNumber, eFrom, eTo)
lns3.scpt
end escape template
lns3.scpt
to uppercase text theText for locale localeCode : missing value
lns3.scpt
if localeCode is missing value then
lns3.scpt
return (asocString's uppercaseString()) as text
lns3.scpt
return (asocString's uppercaseStringWithLocale:(_support's asNSLocaleParameter(localeCode, "for locale"))) as text
lns3.scpt
_error("uppercase text", eText, eNumber, eFrom, eTo)
lns3.scpt
end uppercase text
lns3.scpt
to capitalize text theText for locale localeCode : missing value
lns3.scpt
return (asocString's capitalizedString()) as text
lns3.scpt
return (asocString's capitalizedStringWithLocale:(_support's asNSLocaleParameter(localeCode, "for locale"))) as text
lns3.scpt
_error("capitalize text", eText, eNumber, eFrom, eTo)
lns3.scpt
end capitalize text
lns3.scpt
to lowercase text theText for locale localeCode : missing value
lns3.scpt
return (asocString's lowercaseString()) as text
lns3.scpt
return (asocString's lowercaseStringWithLocale:(_support's asNSLocaleParameter(localeCode, "for locale"))) as text
lns3.scpt
_error("lowercase text", eText, eNumber, eFrom, eTo)
lns3.scpt
end lowercase text
lns3.scpt
to format text templateText using theValues : {}
lns3.scpt
set templateItems to _parseFormatTemplate(_support's asTextParameter(templateText, ""))
lns3.scpt
set i to 2
lns3.scpt
if (count {theValues} each script) = 1 then -- assume it's a script object with getItem handler (e.g. DictionaryCollection)
lns3.scpt
repeat with i from 2 to templateItems's length by 2
lns3.scpt
set item i of templateItems to theValues's getItem(get item i of templateItems) as text
lns3.scpt
else -- it's a list (or a single-item list)
lns3.scpt
set theValues to _support's asListParameter(theValues, "using")
lns3.scpt
set item i of templateItems to (get theValues's item ((get item i of templateItems) as integer)) as text
lns3.scpt
error "Can’t subsitute placeholder “" & item i of templateItems & "”: " & eText number eNumber from eFrom to eTo
lns3.scpt
return _joinText(templateItems, "")
lns3.scpt
_error("format text", eText, eNumber, eFrom, eTo)
lns3.scpt
end format text
lns3.scpt
to transform text theText using nOpts : {OS X line breaks} for locale localeCode : ("none")
lns3.scpt
set nOpts to _support's asListParameter(nOpts, "using")
lns3.scpt
if nOpts is {OS X line breaks} then return _joinText(theText's paragraphs, linefeed)
lns3.scpt
if nOpts is {} then return theText
lns3.scpt
if {OS X line breaks} is in nOpts then
lns3.scpt
set lineBreak to linefeed
lns3.scpt
set lineBreak to missing value
lns3.scpt
if {classic Mac line breaks} is in nOpts then
lns3.scpt
if lineBreak is not missing value then error "Conflicting line break options." number 6502
lns3.scpt
if nOpts's length = 1 then return _joinText(theText's paragraphs, return)
lns3.scpt
set lineBreak to return
lns3.scpt
if {Windows line breaks} is in nOpts then
lns3.scpt
if nOpts's length = 1 then return _joinText(theText's paragraphs, return & linefeed)
lns3.scpt
set lineBreak to return & linefeed
lns3.scpt
set asocString to _support's asNSString(theText)
lns3.scpt
set foldingFlags to 0
lns3.scpt
if {normalized case} is in nOpts then set foldingFlags to foldingFlags + 1
lns3.scpt
if {normalized diacriticals} is in nOpts then set foldingFlags to foldingFlags + 128
lns3.scpt
if {normalized width} is in nOpts then set foldingFlags to foldingFlags + 256
lns3.scpt
if foldingFlags ≠ 0 then set asocString to asocString's stringByFoldingWithOptions:foldingFlags locale:(_support's asNSLocaleParameter(localeCode, "for locale"))
lns3.scpt
if {normalized white space} is in nOpts then
lns3.scpt
set asocString to asocString's stringByReplacingOccurrencesOfString:"\\A(?:\\u2028|\\u2029|\\s)+|(?:\\u2028|\\u2029|\\s)+\\z" withString:"" options:(current application's NSRegularExpressionSearch) range:{0, asocString's |length|()} -- trim leading/trailing white space
lns3.scpt
if lineBreak is missing value then -- also convert line breaks (including Unicode line and paragraph separators) to single spaces
lns3.scpt
set asocString to asocString's stringByReplacingOccurrencesOfString:"(?:\\r\\n|\\r|\\n|\\u2028|\\u2029|\\s)+" withString:space options:(current application's NSRegularExpressionSearch) range:{0, asocString's |length|()}
lns3.scpt
else -- convert line break runs (including any other white space) to single `lineBreak`, and any other white space runs (tabs, spaces, etc) to single spaces
lns3.scpt
set asocString to asocString's stringByReplacingOccurrencesOfString:"(?:\\s*(?:\\r\\n|\\r|\\n|\\u2028|\\u2029))+\\s*" withString:lineBreak options:(current application's NSRegularExpressionSearch) range:{0, asocString's |length|()}
lns3.scpt
set asocString to asocString's stringByReplacingOccurrencesOfString:"[\\f\\t\\p{Z}]+" withString:space options:(current application's NSRegularExpressionSearch) range:{0, asocString's |length|()}
lns3.scpt
else if lineBreak is not missing value then -- standardize line breaks
lns3.scpt
set asocString to asocString's stringByReplacingOccurrencesOfString:"\\r\\n|\\r|\\n|\\u2028|\\u2029" withString:lineBreak options:(current application's NSRegularExpressionSearch) range:{0, asocString's |length|()}
lns3.scpt
if {smart punctuation} is in nOpts then
lns3.scpt
if {typewriter punctuation} is in nOpts or {printable ASCII only} is in nOpts then error "Conflicting punctuation/ASCII options" number 6502
lns3.scpt
set asocString to asocString's stringByApplyingTransform:("Any-Publishing") |reverse|:false
lns3.scpt
else if {typewriter punctuation} is in nOpts then
lns3.scpt
if {printable ASCII only} is in nOpts then error "Conflicting punctuation/ASCII options." number 6502
lns3.scpt
set asocString to asocString's stringByApplyingTransform:("Publishing-Any") |reverse|:false
lns3.scpt
else if {printable ASCII only} is in nOpts then
lns3.scpt
set asocString to asocString's stringByApplyingTransform:("Any-Latin;Latin-ASCII") |reverse|:false
lns3.scpt
set asocString to asocString's stringByReplacingOccurrencesOfString:"[^\\r\\n\\t\\u0020-\\u007e]" withString:"?" options:(current application's NSRegularExpressionSearch) range:{0, asocString's |length|()}
lns3.scpt
if {precomposed canonical form} is in nOpts then
lns3.scpt
set asocString to asocString's precomposedStringWithCanonicalMapping()
lns3.scpt
set didNormalize to true
lns3.scpt
set didNormalize to false
lns3.scpt
if {decomposed canonical form} is in nOpts then
lns3.scpt
if didNormalize then error "Conflicting Unicode options." number 6502
lns3.scpt
set asocString to asocString's decomposedStringWithCanonicalMapping()
lns3.scpt
if {precomposed compatibility form} is in nOpts then
lns3.scpt
set asocString to asocString's precomposedStringWithCompatibilityMapping()
lns3.scpt
if {decomposed compatibility form} is in nOpts then
lns3.scpt
set asocString to asocString's decomposedStringWithCompatibilityMapping()
lns3.scpt
on error eText number 6502
lns3.scpt
_support's throwInvalidParameter(nOpts, "using", list, eText)
lns3.scpt