added strutils.parseEnum; fixes #95

This commit is contained in:
Araq 2012-04-01 00:31:22 +02:00
commit 3ef9f54cc1
4 changed files with 28 additions and 4 deletions

View file

@ -379,6 +379,22 @@ proc parseBool*(s: string): bool =
of "n", "no", "false", "0", "off": result = false of "n", "no", "false", "0", "off": result = false
else: raise newException(EInvalidValue, "cannot interpret as a bool: " & s) else: raise newException(EInvalidValue, "cannot interpret as a bool: " & s)
proc parseEnum*[T: enum](s: string): T =
## parses an enum ``T``. Raises ``EInvalidValue`` for an invalid value in
## `s`. The comparison is done in a style insensitive way.
for e in low(T)..high(T):
if cmpIgnoreStyle(s, $e) == 0:
return e
raise newException(EInvalidValue, "invalid enum value: " & s)
proc parseEnum*[T: enum](s: string, default: T): T =
## parses an enum ``T``. Uses `default` for an invalid value in
## `s`. The comparison is done in a style insensitive way.
for e in low(T)..high(T):
if cmpIgnoreStyle(s, $e) == 0:
return e
result = default
proc repeatChar*(count: int, c: Char = ' '): string {.noSideEffect, proc repeatChar*(count: int, c: Char = ' '): string {.noSideEffect,
rtl, extern: "nsuRepeatChar".} = rtl, extern: "nsuRepeatChar".} =
## Returns a string of length `count` consisting only of ## Returns a string of length `count` consisting only of
@ -1106,4 +1122,7 @@ when isMainModule:
doAssert "-ld a-ldz -ld".replaceWord("-ld") == " a-ldz " doAssert "-ld a-ldz -ld".replaceWord("-ld") == " a-ldz "
doAssert "-lda-ldz -ld abc".replaceWord("-ld") == "-lda-ldz abc" doAssert "-lda-ldz -ld abc".replaceWord("-ld") == "-lda-ldz abc"
type TMyEnum = enum enA, enB, enC, enuD, enE
doAssert parseEnum[TMyEnum]("enu_D") == enuD
doAssert parseEnum("invalid enum value", enC) == enC

View file

@ -2232,4 +2232,3 @@ template eval*(blk: stmt): stmt =
when defined(initDebugger): when defined(initDebugger):
initDebugger() initDebugger()

View file

@ -1,6 +1,7 @@
version 0.9.0 version 0.9.0
============= =============
- test and document AVR/embedded systems better
- make GC realtime capable: GC_step(ms: int) - make GC realtime capable: GC_step(ms: int)
- ``=`` should be overloadable; requires specialization for ``=`` - ``=`` should be overloadable; requires specialization for ``=``
- fix remaining generics bugs - fix remaining generics bugs

View file

@ -12,6 +12,7 @@ Bugfixes
- Fixed a bug where the compiler would "optimize away" valid constant parts of - Fixed a bug where the compiler would "optimize away" valid constant parts of
a string concatenation. a string concatenation.
- Fixed a bug concerning implicit type conversions in ``case`` statements.
Library Additions Library Additions
@ -24,6 +25,9 @@ Library Additions
compile time as if was a macro. compile time as if was a macro.
- Added ``macros.emit`` that can emit an arbitrary computed string as nimrod - Added ``macros.emit`` that can emit an arbitrary computed string as nimrod
code during compilation. code during compilation.
- Added ``strutils.parseEnum``.
- The stdlib can now be avoided to a point where C code generation for 16bit
micro controllers is feasible.
Changes affecting backwards compatibility Changes affecting backwards compatibility
@ -33,7 +37,7 @@ Changes affecting backwards compatibility
The ``system``, ``os``, ``osproc`` and ``memfiles`` modules use the wide The ``system``, ``os``, ``osproc`` and ``memfiles`` modules use the wide
string versions of the WinAPI. Use the ``-d:useWinAnsi`` switch to revert string versions of the WinAPI. Use the ``-d:useWinAnsi`` switch to revert
back to the old behaviour which uses the Ansi string versions. back to the old behaviour which uses the Ansi string versions.
- ``static`` is now a keyword. - ``static`` and ``do`` are now keywords.
- Templates now participate in overloading resolution which can break code that - Templates now participate in overloading resolution which can break code that
uses templates in subtle ways. Use the new ``immediate`` pragma for templates uses templates in subtle ways. Use the new ``immediate`` pragma for templates
to get a template of old behaviour. to get a template of old behaviour.
@ -56,6 +60,7 @@ Language Additions
------------------ ------------------
- Added explicit ``static`` sections for enforced compile time evaluation. - Added explicit ``static`` sections for enforced compile time evaluation.
- Added an alternative notation for lambdas with ``do``.
- ``addr`` is now treated like a prefix operator syntactically. - ``addr`` is now treated like a prefix operator syntactically.