diff options
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/pure/strutils.nim | 13 | ||||
-rw-r--r-- | lib/pure/unicode.nim | 12 | ||||
-rwxr-xr-x | lib/system.nim | 25 | ||||
-rwxr-xr-x | lib/system/jssys.nim | 1 |
4 files changed, 49 insertions, 2 deletions
diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 090ad640c..b5f5a41eb 100755 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -415,7 +415,15 @@ proc parseEnum*[T: enum](s: string, default: T): T = proc repeatChar*(count: int, c: Char = ' '): string {.noSideEffect, rtl, extern: "nsuRepeatChar".} = ## Returns a string of length `count` consisting only of - ## the character `c`. + ## the character `c`. You can use this proc to left align strings. Example: + ## + ## .. code-block:: nimrod + ## let + ## width = 15 + ## text1 = "Hello user!" + ## text2 = "This is a very long string" + ## echo text1 & repeatChar(max(0, width - text1.len)) & "|" + ## echo text2 & repeatChar(max(0, width - text2.len)) & "|" result = newString(count) for i in 0..count-1: result[i] = c @@ -429,7 +437,8 @@ proc align*(s: string, count: int): string {. noSideEffect, rtl, extern: "nsuAlignString".} = ## Aligns a string `s` with spaces, so that is of length `count`. Spaces are ## added before `s` resulting in right alignment. If ``s.len >= count``, no - ## spaces are added and `s` is returned unchanged. + ## spaces are added and `s` is returned unchanged. If you need to left align + ## a string use the repeatChar proc. if s.len < count: result = newString(count) var spaces = count - s.len diff --git a/lib/pure/unicode.nim b/lib/pure/unicode.nim index f76573788..142178a86 100644 --- a/lib/pure/unicode.nim +++ b/lib/pure/unicode.nim @@ -132,6 +132,11 @@ proc toUTF8*(c: TRune): string {.rtl, extern: "nuc$1".} = result = newString(1) result[0] = chr(i) +proc `$`*(runes: seq[TRune]): string = + ## converts a sequence of runes to a string + result = "" + for rune in runes: result.add(rune.toUTF8) + const alphaRanges = [ 0x00d8, 0x00f6, # - @@ -1208,3 +1213,10 @@ proc cmpRunesIgnoreCase*(a, b: string): int {.rtl, extern: "nuc$1", procvar.} = result = irune(toLower(ar)) - irune(toLower(br)) if result != 0: return result = a.len - b.len + +when isMainModule: + let + someString = "öÑ" + someRunes = @[runeAt(someString, 0), runeAt(someString, 2)] + compared = (someString == $someRunes) + assert compared == true diff --git a/lib/system.nim b/lib/system.nim index 4aa7ecad5..8885de624 100755 --- a/lib/system.nim +++ b/lib/system.nim @@ -370,9 +370,34 @@ proc newSeq*[T](s: var seq[T], len: int) {.magic: "NewSeq", noSideEffect.} ## creates a new sequence of type ``seq[T]`` with length ``len``. ## This is equivalent to ``s = @[]; setlen(s, len)``, but more ## efficient since no reallocation is needed. + ## + ## Note that the sequence will be filled with uninitialized entries, which + ## can be a problem for sequences containing strings. After the creation of + ## the sequence you should assign entries to the sequence instead of adding + ## them. Example: + ## + ## .. code-block:: nimrod + ## var inputStrings : seq[string] + ## newSeq(inputStrings, 3) + ## inputStrings[0] = "The fourth" + ## inputStrings[1] = "assignment" + ## inputStrings[2] = "would crash" + ## #inputStrings[3] = "out of bounds" proc newSeq*[T](len = 0): seq[T] = ## creates a new sequence of type ``seq[T]`` with length ``len``. + ## + ## Note that the sequence will be filled with uninitialized entries, which + ## can be a problem for sequences containing strings. After the creation of + ## the sequence you should assign entries to the sequence instead of adding + ## them. Example: + ## + ## .. code-block:: nimrod + ## var inputStrings = newSeq[string](3) + ## inputStrings[0] = "The fourth" + ## inputStrings[1] = "assignment" + ## inputStrings[2] = "would crash" + ## #inputStrings[3] = "out of bounds" newSeq(result, len) proc len*[TOpenArray: openArray|varargs](x: TOpenArray): int {. diff --git a/lib/system/jssys.nim b/lib/system/jssys.nim index 789e39d6d..1c43bfdc7 100755 --- a/lib/system/jssys.nim +++ b/lib/system/jssys.nim @@ -491,6 +491,7 @@ proc toU32(a: int): int32 {.noStackFrame, compilerproc.} = proc nimMin(a, b: int): int {.compilerproc.} = return if a <= b: a else: b proc nimMax(a, b: int): int {.compilerproc.} = return if a >= b: a else: b +type NimString = string # hack for hti.nim include "system/hti" proc isFatPointer(ti: PNimType): bool = |