Obsolete test build - format() - 1.1.16.05-13+g79cee0b

Community news and information about new or upcoming versions of AutoHotkey
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Obsolete test build - format() - 1.1.16.05-13+g79cee0b

15 Oct 2014, 03:14

Obsolete: see v1.1.17.

I have uploaded a test build with a new function:

string := Format(format, values*)

The format string should contain placeholders of the form {param:format}.
param: An integer corresponding to the position of a value in the parameter list, where 1 is the first parameter after format. If omitted, the number of the last placeholder + 1 is used (or 1 for the first placeholder).
format: A string controlling how the value will be formatted. The following subset of the printf format specification is supported:
  • flags: - + 0 space #
    width: decimal integer only
    .precision: decimal integer only
    type: d i o u x X e E f g G a A c C p s (not n S Z)
Size prefixes are not supported - numbers are always 64-bit, except the character and pointer types.

type defaults to 's' if omitted. 's' is a string value, not a pointer to a string.

As with Send, {{} and {}} produce literal braces. Any invalid placeholders are put into the output as is. Whitespace inside the braces is not permitted (except as a flag).

Examples:

Code: Select all

; Simple substitution
s .= format("{2}, {1}!`r`n", "World", "Hello")
; Padding with spaces
s .= format("|{:-10}|`r`n|{:10}|`r`n", "Left", "Right")
; Hexadecimal
s .= format("{1:#x} {2:X} 0x{3:x}`r`n", 3735928559, 195948557, 0)
; Floating-point
s .= format("{1:0.3f} {1:.10f}", 4*atan(1))

ListVars
WinWaitActive ahk_class AutoHotkey
ControlSetText Edit1, %s%
WinWaitClose
The differences from format() distributed with v2 alpha include:
  • The parameter index can be omitted, as in {} or {:02i}
  • The type can be omitted (defaults to 's'), as in {1:5}.
  • Size prefixes aren't supported, as mentioned above.
This also contains everything from the previous test build.

Download

---

Hexadecimal integers are always unsigned, as per normal printf behaviour but inconsistent with AutoHotkey's normal hexadecimal formatting. The '#' flag adds the 0x prefix only for non-zero values, again inconsistent with the norm. This is because the formatting of each value is done entirely by printf (to do otherwise would mean potentially much larger code and longer development time).
User avatar
fincs
Posts: 527
Joined: 30 Sep 2013, 14:17
Location: Seville, Spain
Contact:

Re: New test build - format() - 1.1.16.05-13+g79cee0b

15 Oct 2014, 13:49

I'm glad you rebased the changes of your earlier test build on top of the latest v1.1 release. The built-in Format() function will be really useful (no more ugly SetFormat temp hacks!).

BTW: for consistency, shouldn't it be called StrFormat()?
fincs
Windows 11 Pro (Version 22H2) | AMD Ryzen 7 3700X with 32 GB of RAM | AutoHotkey v2.0.0 + v1.1.36.02
Get SciTE4AutoHotkey v3.1.0 - [My project list]
arcticir
Posts: 694
Joined: 17 Nov 2013, 11:32

Re: New test build - format() - 1.1.16.05-13+g79cee0b

15 Oct 2014, 15:39

See this update, I am reminded of another function on the string, I always want to achieve it.
StringToObj()

Such as:

Code: Select all

Test:="key2"
Test2:="3"
;obj:={1:"key1",2:Test,(Test2):"key3"}	;It is not JSON rules, is AHK obj rules.
s={1:"key1",2:Test,(Test2):"key3"}
obj:=StringToObj(s)
MsgBox % ObjTree(obj)
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: New test build - format() - 1.1.16.05-13+g79cee0b

15 Oct 2014, 18:52

fincs wrote:BTW: for consistency, shouldn't it be called StrFormat()?
I thought about that, but it doesn't seem necessary, just inconvenient. We also have FormatTime, which also produces a formatted string, and SplitPath and Sort, which manipulate strings but don't begin with "Str". Aside from that, it doesn't format a string; it produces a formatted string from zero, one or more values.

Edit: I'm also thinking about adding a simple hex() function, since there's a fairly common need and format("0x{:x}", v) is a bit awkward. (format("{:#x}", v) is slightly better but produces 0 instead of 0x0.)
Last edited by lexikos on 15 Oct 2014, 19:19, edited 1 time in total.
Reason: .
User avatar
joedf
Posts: 8953
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: New test build - format() - 1.1.16.05-13+g79cee0b

16 Oct 2014, 03:44

A built in Str2hex or hex() function would be very useful
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

Return to “Announcements”

Who is online

Users browsing this forum: No registered users and 59 guests