stdlib uses more of varargs

This commit is contained in:
Araq 2012-08-16 16:30:13 +02:00
commit d37fe6e0a5
4 changed files with 14 additions and 4 deletions

View file

@ -1015,7 +1015,7 @@ proc findNormalized(x: string, inArray: openarray[string]): int =
proc invalidFormatString() {.noinline.} = proc invalidFormatString() {.noinline.} =
raise newException(EInvalidValue, "invalid format string") raise newException(EInvalidValue, "invalid format string")
proc addf*(s: var string, formatstr: string, a: openarray[string]) {. proc addf*(s: var string, formatstr: string, a: varargs[string, `$`]) {.
noSideEffect, rtl, extern: "nsuAddf".} = noSideEffect, rtl, extern: "nsuAddf".} =
## The same as ``add(s, formatstr % a)``, but more efficient. ## The same as ``add(s, formatstr % a)``, but more efficient.
const PatternChars = {'a'..'z', 'A'..'Z', '0'..'9', '\128'..'\255', '_'} const PatternChars = {'a'..'z', 'A'..'Z', '0'..'9', '\128'..'\255', '_'}

View file

@ -297,7 +297,7 @@ proc subex*(s: string): TSubex =
## no syntax checking but this may change in later versions. ## no syntax checking but this may change in later versions.
result = TSubex(s) result = TSubex(s)
proc addf*(s: var string, formatstr: TSubex, a: openarray[string]) {. proc addf*(s: var string, formatstr: TSubex, a: varargs[string, `$`]) {.
noSideEffect, rtl, extern: "nfrmtAddf".} = noSideEffect, rtl, extern: "nfrmtAddf".} =
## The same as ``add(s, formatstr % a)``, but more efficient. ## The same as ``add(s, formatstr % a)``, but more efficient.
var p: TFormatParser var p: TFormatParser
@ -326,6 +326,15 @@ proc `%` *(formatstr: TSubex, a: string): string {.noSideEffect,
result = newStringOfCap(formatstr.string.len + a.len) result = newStringOfCap(formatstr.string.len + a.len)
addf(result, formatstr, [a]) addf(result, formatstr, [a])
proc format*(formatstr: TSubex, a: varargs[string, `$`]): string {.noSideEffect,
rtl, extern: "nfrmtFormatVarargs".} =
## The `substitution`:idx: operator performs string substitutions in
## `formatstr` and returns a modified `formatstr`. This is often called
## `string interpolation`:idx:.
##
result = newStringOfCap(formatstr.string.len + a.len shl 4)
addf(result, formatstr, a)
{.pop.} {.pop.}
when isMainModule: when isMainModule:

View file

@ -3,6 +3,7 @@ version 0.9.0
- implement "closure tuple consists of a single 'ref'" optimization - implement "closure tuple consists of a single 'ref'" optimization
- implement for loop transformation for first class iterators - implement for loop transformation for first class iterators
- make templates hygienic by default: 'gensym', 'inject' pragmas
- implicit deref for parameter matching - implicit deref for parameter matching
- ``final`` should be the default for objects - ``final`` should be the default for objects
@ -41,8 +42,6 @@ version 0.9.XX
- ``hoist`` pragma for loop hoisting - ``hoist`` pragma for loop hoisting
- document destructors; don't work yet when used as expression - document destructors; don't work yet when used as expression
- make use of ``tyIter`` to fix the implicit items/pairs issue - make use of ``tyIter`` to fix the implicit items/pairs issue
- make templates hygienic by default: try to gensym() everything in the 'block'
of a template; find a better solution for gensym instead of `*ident`
- introduce 'callsite' magic and make macros and templates the same - introduce 'callsite' magic and make macros and templates the same
- better support for macros that rewrite procs - better support for macros that rewrite procs
- macros need access to types and symbols - macros need access to types and symbols

View file

@ -63,6 +63,8 @@ Library Additions
only support fixed length arrays). only support fixed length arrays).
- Added ``system.compiles`` which can be used to check whether a type supports - Added ``system.compiles`` which can be used to check whether a type supports
some operation. some operation.
- Added ``strutils.format``, ``subexes.format`` which use the
new ``varargs`` type.
Changes affecting backwards compatibility Changes affecting backwards compatibility