Increment a numerical string: Difference between revisions
Content deleted Content added
→{{header|AppleScript}}: Added AppleScriptObjC solution. |
|||
Line 398: | Line 398: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
===Functional=== |
|||
Preserving the distinction between real and integer strings, and allowing for strings containing non-numeric tokens and/or multiple numeric expressions. Provides an option to either retain or prune out any non-numeric tokens in the string: |
Preserving the distinction between real and integer strings, and allowing for strings containing non-numeric tokens and/or multiple numeric expressions. Provides an option to either retain or prune out any non-numeric tokens in the string: |
||
{{Trans|Python}} |
{{Trans|Python}} |
||
Line 512: | Line 513: | ||
<pre>42 1492.3 -0.5 137 |
<pre>42 1492.3 -0.5 137 |
||
42 pine martens in 1492.3 -0.5 mushrooms ≠ 137</pre> |
42 pine martens in 1492.3 -0.5 mushrooms ≠ 137</pre> |
||
---- |
|||
===AppleScriptObjC=== |
|||
The task description's delightfully unspecific about what's meant by "increment" ("add 1" as in C-related languages or "add a certain amount" as in English?) and what's meant by "numerical string" with regard to format, number type, locale, and base. This attempts to cover most bases — except for base itself, which is strictly decimal here. |
|||
<syntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later |
|||
use framework "Foundation" |
|||
-- Using the machine's own locale setting. |
|||
on incrementNumericalString:str byAmount:incrementSize |
|||
set localeID to current application's class "NSLocale"'s currentLocale()'s localeIdentifier() |
|||
return my incrementNumericalString:str byAmount:incrementSize localeID:localeID |
|||
end incrementNumericalString:byAmount: |
|||
-- Including a locale ID as a parameter. |
|||
on incrementNumericalString:str byAmount:incrementSize localeID:localeID |
|||
set |⌘| to current application |
|||
set str to |⌘|'s class "NSString"'s stringWithString:(str) |
|||
set locale to |⌘|'s class "NSLocale"'s localeWithLocaleIdentifier:(localeID) |
|||
-- We'll use an NSNumberFormatter to generate NSDecimalNumber objects for the math |
|||
-- as its string conversion methods are more flexible than NSDecimalNumber's own. |
|||
tell |⌘|'s class "NSNumberFormatter"'s new() |
|||
its setGeneratesDecimalNumbers:(true) |
|||
its setLocale:(locale) |
|||
set eRange to str's rangeOfString:("[Ee]") options:(|⌘|'s NSRegularExpressionSearch) |
|||
if (eRange's |length|() > 0) then |
|||
-- "E" or "e" in the input string. Set the output style to "scientific". |
|||
its setNumberStyle:(|⌘|'s NSNumberFormatterScientificStyle) |
|||
set e to str's substringWithRange:(eRange) |
|||
its setExponentSymbol:(e) -- Same case as the original. |
|||
else |
|||
-- Straight base 10 numerical text, with or without separators as per the input and locale. |
|||
its setNumberStyle:(|⌘|'s NSNumberFormatterDecimalStyle) |
|||
set groupingSeparator to locale's objectForKey:(|⌘|'s NSLocaleGroupingSeparator) |
|||
its setUsesGroupingSeparator:(str's containsString:(groupingSeparator)) |
|||
end if |
|||
-- Generate and add the NSDecimalNumbers and convert the result back to NSString. |
|||
set decNum to its numberFromString:(str) |
|||
set increment to its numberFromString:(incrementSize as text) |
|||
set output to its stringFromNumber:(decNum's decimalNumberByAdding:(increment)) |
|||
end tell |
|||
-- If the input style's scientific and has a "+" after the "E" or "e" , insert a "+" at that point |
|||
-- in the output if there's no sign there already. NSNumberFormatter only includes minus signs. |
|||
if ((eRange's |length|() > 0) and ((str's containsString:((e as text) & "+")) as boolean)) then ¬ |
|||
set output to output's stringByReplacingOccurrencesOfString:("(?<=[Ee])(?=\\d)") withString:("+") ¬ |
|||
options:(|⌘|'s NSRegularExpressionSearch) range:({0, output's |length|()}) |
|||
return output as text -- Return as AppleScript text. |
|||
end incrementNumericalString:byAmount:localeID: |
|||
return {¬ |
|||
(my incrementNumericalString:"12345" byAmount:1), ¬ |
|||
(my incrementNumericalString:"999,999,999,999,999" byAmount:5), ¬ |
|||
(my incrementNumericalString:"-1.23" byAmount:10 localeID:"en"), ¬ |
|||
(my incrementNumericalString:"-1,23E+0" byAmount:"10" localeID:"fr_FR") ¬ |
|||
}</syntaxhighlight> |
|||
{{output}} |
|||
<syntaxhighlight lang="applescript">{"12346", "1,000,000,000,000,004", "8.77", "8,77E+0"}</syntaxhighlight> |
|||
=={{header|ARM Assembly}}== |
=={{header|ARM Assembly}}== |