diff options
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/macros.nim | 42 | ||||
-rw-r--r-- | lib/pure/os.nim | 22 | ||||
-rw-r--r-- | lib/pure/osproc.nim | 6 | ||||
-rw-r--r-- | lib/pure/regexprs.nim | 22 | ||||
-rw-r--r-- | lib/pure/streams.nim | 4 | ||||
-rw-r--r-- | lib/pure/strutils.nim | 68 |
6 files changed, 113 insertions, 51 deletions
diff --git a/lib/pure/macros.nim b/lib/pure/macros.nim index ce026a832..644a785cf 100644 --- a/lib/pure/macros.nim +++ b/lib/pure/macros.nim @@ -45,27 +45,27 @@ type nnkCurly, nnkBracket, nnkBracketExpr, nnkPragmaExpr, nnkRange, nnkDotExpr, nnkCheckedFieldExpr, nnkDerefExpr, nnkIfExpr, nnkElifExpr, nnkElseExpr, nnkLambda, - nnkAccQuoted, nnkHeaderQuoted, nnkTableConstr, nnkQualified, - nnkHiddenStdConv, nnkHiddenSubConv, nnkHiddenCallConv, nnkConv, - nnkCast, nnkAddr, nnkHiddenAddr, nnkHiddenDeref, - nnkObjDownConv, nnkObjUpConv, nnkChckRangeF, nnkChckRange64, - nnkChckRange, nnkStringToCString, nnkCStringToString, nnkPassAsOpenArray, - nnkAsgn, nnkFastAsgn, nnkDefaultTypeParam, nnkGenericParams, - nnkFormalParams, nnkOfInherit, nnkModule, nnkProcDef, - nnkConverterDef, nnkMacroDef, nnkTemplateDef, nnkIteratorDef, - nnkOfBranch, nnkElifBranch, nnkExceptBranch, nnkElse, - nnkMacroStmt, nnkAsmStmt, nnkPragma, nnkIfStmt, - nnkWhenStmt, nnkForStmt, nnkWhileStmt, nnkCaseStmt, - nnkVarSection, nnkConstSection, nnkConstDef, nnkTypeSection, - nnkTypeDef, nnkYieldStmt, nnkTryStmt, nnkFinally, - nnkRaiseStmt, nnkReturnStmt, nnkBreakStmt, nnkContinueStmt, - nnkBlockStmt, nnkDiscardStmt, nnkStmtList, nnkImportStmt, - nnkFromStmt, nnkImportAs, nnkIncludeStmt, nnkCommentStmt, - nnkStmtListExpr, nnkBlockExpr, nnkStmtListType, nnkBlockType, - nnkVm, nnkTypeOfExpr, nnkObjectTy, nnkTupleTy, - nnkRecList, nnkRecCase, nnkRecWhen, nnkRefTy, - nnkPtrTy, nnkVarTy, nnkAbstractTy, nnkProcTy, - nnkEnumTy, nnkEnumFieldDef, nnkReturnToken + nnkAccQuoted, nnkTableConstr, nnkQualified, nnkBind, + nnkSymChoice, nnkHiddenStdConv, nnkHiddenSubConv, nnkHiddenCallConv, + nnkConv, nnkCast, nnkAddr, nnkHiddenAddr, + nnkHiddenDeref, nnkObjDownConv, nnkObjUpConv, nnkChckRangeF, + nnkChckRange64, nnkChckRange, nnkStringToCString, nnkCStringToString, + nnkPassAsOpenArray, nnkAsgn, nnkFastAsgn, nnkDefaultTypeParam, + nnkGenericParams, nnkFormalParams, nnkOfInherit, nnkModule, + nnkProcDef, nnkConverterDef, nnkMacroDef, nnkTemplateDef, + nnkIteratorDef, nnkOfBranch, nnkElifBranch, nnkExceptBranch, + nnkElse, nnkMacroStmt, nnkAsmStmt, nnkPragma, + nnkIfStmt, nnkWhenStmt, nnkForStmt, nnkWhileStmt, + nnkCaseStmt, nnkVarSection, nnkConstSection, nnkConstDef, + nnkTypeSection, nnkTypeDef, nnkYieldStmt, nnkTryStmt, + nnkFinally, nnkRaiseStmt, nnkReturnStmt, nnkBreakStmt, + nnkContinueStmt, nnkBlockStmt, nnkDiscardStmt, nnkStmtList, + nnkImportStmt, nnkFromStmt, nnkImportAs, nnkIncludeStmt, + nnkCommentStmt, nnkStmtListExpr, nnkBlockExpr, nnkStmtListType, + nnkBlockType, nnkVm, nnkTypeOfExpr, nnkObjectTy, + nnkTupleTy, nnkRecList, nnkRecCase, nnkRecWhen, + nnkRefTy, nnkPtrTy, nnkVarTy, nnkAbstractTy, + nnkProcTy, nnkEnumTy, nnkEnumFieldDef, nnkReturnToken TNimNodeKinds* = set[TNimrodNodeKind] TNimrodTypeKind* = enum ntyNone, ntyBool, ntyChar, ntyEmpty, diff --git a/lib/pure/os.nim b/lib/pure/os.nim index 847117d93..a0e5565e8 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -556,9 +556,9 @@ proc sameFileContent*(path1, path2: string): bool = bufSize = 8192 # 8K buffer var a, b: TFile - if not openFile(a, path1): return false - if not openFile(b, path2): - closeFile(a) + if not open(a, path1): return false + if not open(b, path2): + close(a) return false var bufA = alloc(bufsize) var bufB = alloc(bufsize) @@ -576,8 +576,8 @@ proc sameFileContent*(path1, path2: string): bool = if readA != bufSize: break # end of file dealloc(bufA) dealloc(bufB) - closeFile(a) - closeFile(b) + close(a) + close(b) proc copyFile*(dest, source: string) = ## Copies a file from `source` to `dest`. If this fails, @@ -590,9 +590,9 @@ proc copyFile*(dest, source: string) = bufSize = 8192 # 8K buffer var d, s: TFile - if not openFile(s, source): OSError() - if not openFile(d, dest, fmWrite): - closeFile(s) + if not open(s, source): OSError() + if not open(d, dest, fmWrite): + close(s) OSError() var buf: Pointer = alloc(bufsize) @@ -603,8 +603,8 @@ proc copyFile*(dest, source: string) = if bytesread != bufSize: break if bytesread != bytesWritten: OSError() dealloc(buf) - closeFile(s) - closeFile(d) + close(s) + close(d) proc moveFile*(dest, source: string) = ## Moves a file from `source` to `dest`. If this fails, `EOS` is raised. @@ -954,7 +954,7 @@ proc getHomeDir*(): string = when defined(windows): return getEnv("USERPROFILE") & "\\" else: return getEnv("HOME") & "/" -proc getConfigDir*(): string {.noSideEffect.} = +proc getConfigDir*(): string = ## Returns the config directory of the current user for applications. when defined(windows): return getEnv("APPDATA") & "\\" else: return getEnv("HOME") & "/.config/" diff --git a/lib/pure/osproc.nim b/lib/pure/osproc.nim index 205460614..cf4d926b6 100644 --- a/lib/pure/osproc.nim +++ b/lib/pure/osproc.nim @@ -405,17 +405,17 @@ else: proc inputStream(p: PProcess): PStream = var f: TFile - if not openFile(f, p.inputHandle, fmWrite): OSError() + if not open(f, p.inputHandle, fmWrite): OSError() result = newFileStream(f) proc outputStream(p: PProcess): PStream = var f: TFile - if not openFile(f, p.outputHandle, fmRead): OSError() + if not open(f, p.outputHandle, fmRead): OSError() result = newFileStream(f) proc errorStream(p: PProcess): PStream = var f: TFile - if not openFile(f, p.errorHandle, fmRead): OSError() + if not open(f, p.errorHandle, fmRead): OSError() result = newFileStream(f) proc csystem(cmd: cstring): cint {.nodecl, importc: "system".} diff --git a/lib/pure/regexprs.nim b/lib/pure/regexprs.nim index e2aac3d17..69b35d7f0 100644 --- a/lib/pure/regexprs.nim +++ b/lib/pure/regexprs.nim @@ -152,3 +152,25 @@ const ## common regular expressions reURL* = r"\b(http(s)?|ftp|gopher|telnet|file|notes|ms\-help):" & r"((//)|(\\\\))+[\w\d:#@%/;$()~_?\+\-\=\\\.\&]*\b" ## describes an URL + +proc verbose*(pattern: string): string {.noSideEffect.} = + ## deletes whitespace from a pattern that is not escaped or in a character + ## class. This is modelled after Perl's ``/x`` modifier. + result = "" + var i = 0 + while i < pattern.len: + case pattern[i] + of ' ', '\t': + inc i + of '\\': + add result, '\\' + add result, pattern[i+1] + inc i, 2 + of '[': + while pattern[i] != ']' and pattern[i] != '\0': + add result, pattern[i] + inc i + else: + add result, pattern[i] + inc i + diff --git a/lib/pure/streams.nim b/lib/pure/streams.nim index 238cba4ec..0f4d4f771 100644 --- a/lib/pure/streams.nim +++ b/lib/pure/streams.nim @@ -144,7 +144,7 @@ type TFileStream* = object of TStream f: TFile -proc fsClose(s: PFileStream) = closeFile(s.f) +proc fsClose(s: PFileStream) = close(s.f) proc fsAtEnd(s: PFileStream): bool = return EndOfFile(s.f) proc fsSetPosition(s: PFileStream, pos: int) = setFilePos(s.f, pos) proc fsGetPosition(s: PFileStream): int = return int(getFilePos(s.f)) @@ -171,7 +171,7 @@ proc newFileStream*(filename: string, mode: TFileMode): PFileStream = ## creates a new stream from the file named `filename` with the mode `mode`. ## If the file cannot be opened, nil is returned. var f: TFile - if OpenFile(f, filename, mode): result = newFileStream(f) + if Open(f, filename, mode): result = newFileStream(f) when true: diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index a4895110b..3a9a8d0b3 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -150,15 +150,18 @@ proc find*(s: string, chars: set[char], start: int = 0): int {.noSideEffect.} ## Searches for `chars` in `s` starting at position `start`. If `s` contains ## none of the characters in `chars`, -1 is returned. -proc replaceStr*(s, sub, by: string): string {.noSideEffect.} +proc replaceStr*(s, sub, by: string): string {.noSideEffect, deprecated.} ## Replaces `sub` in `s` by the string `by`. + ## **Deprecated since version 0.8.0**: Use `replace` instead. -proc replaceStr*(s: string, sub, by: char): string {.noSideEffect.} +proc replaceStr*(s: string, sub, by: char): string {.noSideEffect, deprecated.} ## optimized version for characters. + ## **Deprecated since version 0.8.0**: Use `replace` instead. -proc deleteStr*(s: var string, first, last: int) +proc deleteStr*(s: var string, first, last: int) {.deprecated.} ## Deletes in `s` the characters at position `first`..`last`. This modifies ## `s` itself, it does not return a copy. + ## **Deprecated since version 0.8.0**: Use `delete` instead. proc toOctal*(c: char): string ## Converts a character `c` to its octal representation. The resulting @@ -185,7 +188,7 @@ iterator split*(s: string, seps: set[char] = Whitespace): string = ## for word in split(";;this;is;an;;example;;;", {';'}): ## writeln(stdout, word) ## - ## produces in the same output. + ## produces the same output. var first: int = 0 last: int = 0 @@ -315,17 +318,15 @@ proc intToStr*(x: int, minchars: int = 1): string proc ParseInt*(s: string): int {.noSideEffect.} ## Parses a decimal integer value contained in `s`. If `s` is not ## a valid integer, `EInvalidValue` is raised. - # XXX: make this biggestint! proc ParseBiggestInt*(s: string): biggestInt {.noSideEffect.} ## Parses a decimal integer value contained in `s`. If `s` is not ## a valid integer, `EInvalidValue` is raised. -proc ParseFloat*(s: string, start = 0): float {.noSideEffect.} +proc ParseFloat*(s: string): float {.noSideEffect.} ## Parses a decimal floating point value contained in `s`. If `s` is not ## a valid floating point number, `EInvalidValue` is raised. ``NAN``, ## ``INF``, ``-INF`` are also supported (case insensitive comparison). - # XXX: make this biggestfloat. # the stringify and format operators: proc toString*[Ty](x: Ty): string @@ -439,10 +440,15 @@ proc findNormalized(x: string, inArray: openarray[string]): int = proc addf(s: var string, formatstr: string, a: openarray[string]) = const PatternChars = {'a'..'z', 'A'..'Z', '0'..'9', '\128'..'\255', '_'} var i = 0 + var num = 0 while i < len(formatstr): if formatstr[i] == '$': case formatstr[i+1] # again we use the fact that strings # are zero-terminated here + of '#': + add s, a[num] + inc i, 2 + inc num of '$': add s, '$' inc(i, 2) @@ -452,6 +458,7 @@ proc addf(s: var string, formatstr: string, a: openarray[string]) = while formatstr[i] in {'0'..'9'}: j = j * 10 + ord(formatstr[i]) - ord('0') inc(i) + num = j add s, a[j - 1] of '{': var j = i+1 @@ -510,6 +517,31 @@ proc splitSeq(s: string, seps: set[char]): seq[string] = # --------------------------------------------------------------------------- +proc join*(a: openArray[string], sep: string): string = + ## concatenates all strings in `a` separating them with `sep`. + if len(a) > 0: + var L = sep.len * (a.len-1) + for i in 0..high(a): inc(L, a[i].len) + result = newString(L) + setLen(result, 0) + add(result, a[0]) + for i in 1..high(a): + add(result, sep) + add(result, a[i]) + else: + result = "" + +proc join*(a: openArray[string]): string = + ## concatenates all strings in `a`. + if len(a) > 0: + var L = 0 + for i in 0..high(a): inc(L, a[i].len) + result = newString(L) + setLen(result, 0) + for i in 0..high(a): add(result, a[i]) + else: + result = "" + proc strip(s: string, leading = true, trailing = true): string = const chars: set[Char] = Whitespace @@ -638,7 +670,8 @@ proc contains(s: string, c: char): bool = proc contains(s, sub: string): bool = return find(s, sub) >= 0 -proc replaceStr(s, sub, by: string): string = +proc replace*(s, sub, by: string): string = + ## Replaces `sub` in `s` by the string `by`. var a: TSkipTable result = "" preprocessSub(sub, a) @@ -652,7 +685,8 @@ proc replaceStr(s, sub, by: string): string = # copy the rest: add result, copy(s, i) -proc replaceStr(s: string, sub, by: char): string = +proc replace*(s: string, sub, by: char): string = + ## optimized version for characters. result = newString(s.len) var i = 0 while i < s.len: @@ -660,17 +694,23 @@ proc replaceStr(s: string, sub, by: char): string = else: result[i] = s[i] inc(i) -proc deleteStr(s: var string, first, last: int) = +proc delete*(s: var string, first, last: int) = + ## Deletes in `s` the characters at position `first`..`last`. This modifies + ## `s` itself, it does not return a copy. + var + i = first # example: "abc___uvwxyz\0" (___ is to be deleted) # --> first == 3, last == 5 # s[first..] = s[last+1..] - var - i = first while last+i+1 < len(s): s[i] = s[last+i+1] inc(i) setlen(s, len(s)-(last-first+1)) +proc replaceStr(s, sub, by: string): string = return replace(s, sub, by) +proc replaceStr(s: string, sub, by: char): string = return replace(s, sub, by) +proc deleteStr*(s: var string, first, last: int) = delete(s, first, last) + # parsing numbers: proc toHex(x: BiggestInt, len: int): string = @@ -734,11 +774,11 @@ proc ParseBiggestInt(s: string): biggestInt = if index == -1: raise newException(EInvalidValue, "invalid integer: " & s) -proc ParseFloat(s: string, start = 0): float = +proc ParseFloat(s: string): float = var esign = 1.0 sign = 1.0 - i = start + i = 0 exponent: int flags: int result = 0.0 |