summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pure')
-rwxr-xr-xlib/pure/cgi.nim19
-rwxr-xr-xlib/pure/cookies.nim4
-rwxr-xr-xlib/pure/httpclient.nim6
-rwxr-xr-xlib/pure/httpserver.nim12
-rwxr-xr-xlib/pure/json.nim3
-rwxr-xr-xlib/pure/os.nim34
-rwxr-xr-xlib/pure/parseopt.nim2
-rwxr-xr-xlib/pure/parseutils.nim8
-rwxr-xr-xlib/pure/pegs.nim16
-rwxr-xr-xlib/pure/regexprs.nim2
-rwxr-xr-xlib/pure/ropes.nim4
-rwxr-xr-xlib/pure/strtabs.nim4
-rwxr-xr-xlib/pure/strutils.nim46
-rwxr-xr-xlib/pure/xmldom.nim4
14 files changed, 89 insertions, 75 deletions
diff --git a/lib/pure/cgi.nim b/lib/pure/cgi.nim
index af222caba..ae05d5734 100755
--- a/lib/pure/cgi.nim
+++ b/lib/pure/cgi.nim
@@ -36,7 +36,7 @@ proc URLencode*(s: string): string =
   ## ``{'A'..'Z', 'a'..'z', '0'..'9', '_'}`` are carried over to the result,
   ## a space is converted to ``'+'`` and every other character is encoded as
   ## ``'%xx'`` where ``xx`` denotes its hexadecimal value. 
-  result = ""
+  result = newStringOfCap(s.len + s.len shr 2) # assume 12% non-alnum-chars
   for i in 0..s.len-1:
     case s[i]
     of 'a'..'z', 'A'..'Z', '0'..'9', '_': add(result, s[i])
@@ -57,8 +57,9 @@ proc URLdecode*(s: string): string =
   ## is converted to a space, ``'%xx'`` (where ``xx`` denotes a hexadecimal
   ## value) is converted to the character with ordinal number ``xx``, and  
   ## and every other character is carried over. 
-  result = ""
+  result = newString(s.len)
   var i = 0
+  var j = 0
   while i < s.len:
     case s[i]
     of '%': 
@@ -66,10 +67,12 @@ proc URLdecode*(s: string): string =
       handleHexChar(s[i+1], x)
       handleHexChar(s[i+2], x)
       inc(i, 2)
-      add(result, chr(x))
-    of '+': add(result, ' ')
-    else: add(result, s[i])
+      result[j] = chr(x)
+    of '+': result[j] = ' '
+    else: result[j] = s[i]
     inc(i)
+    inc(j)
+  setLen(result, j)
 
 proc addXmlChar(dest: var string, c: Char) {.inline.} = 
   case c
@@ -86,7 +89,7 @@ proc XMLencode*(s: string): string =
   ## * ``>`` is replaced by ``&gt;``
   ## * ``&`` is replaced by ``&amp;``
   ## * every other character is carried over.
-  result = ""
+  result = newStringOfCap(s.len + s.len shr 2)
   for i in 0..len(s)-1: addXmlChar(result, s[i])
 
 type
@@ -367,4 +370,8 @@ proc existsCookie*(name: string): bool =
   if gcookies == nil: gcookies = parseCookies(getHttpCookie())
   result = hasKey(gcookies, name)
 
+when isMainModule:
+  const test1 = "abc\L+def xyz"
+  assert UrlEncode(test1) == "abc%0A%2Bdef+xyz"
+  assert UrlDecode(UrlEncode(test1)) == test1
 
diff --git a/lib/pure/cookies.nim b/lib/pure/cookies.nim
index eed6c7512..8e5de008c 100755
--- a/lib/pure/cookies.nim
+++ b/lib/pure/cookies.nim
@@ -1,7 +1,7 @@
 #
 #
 #            Nimrod's Runtime Library
-#        (c) Copyright 2010 Andreas Rumpf
+#        (c) Copyright 2011 Andreas Rumpf
 #
 #    See the file "copying.txt", included in this
 #    distribution, for details about the copyright.
@@ -24,7 +24,7 @@ proc parseCookies*(s: string): PStringTable =
     inc(i) # skip '='
     var valstart = i
     while s[i] != ';' and s[i] != '\0': inc(i)
-    result[copy(s, keystart, keyend)] = copy(s, valstart, i-1)
+    result[substr(s, keystart, keyend)] = substr(s, valstart, i-1)
     if s[i] == '\0': break
     inc(i) # skip ';'
 
diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim
index 39ceb5f68..73a8cb853 100755
--- a/lib/pure/httpclient.nim
+++ b/lib/pure/httpclient.nim
@@ -109,7 +109,7 @@ proc parseChunks(d: var string, start: int, s: TSocket): string =
     if charAt(d, i, s) == '\L': inc(i)
     else: httpError("CR-LF after chunksize expected")
     
-    var x = copy(d, i, i+chunkSize-1)
+    var x = substr(d, i, i+chunkSize-1)
     var size = x.len
     result.add(x)
     inc(i, size)
@@ -133,7 +133,7 @@ proc parseBody(d: var string, start: int, s: TSocket,
   if headers["Transfer-Encoding"] == "chunked":
     result = parseChunks(d, start, s)
   else:
-    result = copy(d, start)
+    result = substr(d, start)
     # -REGION- Content-Length
     # (http://tools.ietf.org/html/rfc2616#section-4.4) NR.3
     var contentLengthHeader = headers["Content-Length"]
@@ -236,7 +236,7 @@ proc request*(url: string, httpMethod = httpGET, extraHeaders = "",
   ## | Extra headers can be specified and must be seperated by ``\c\L``
   var r = parseUrl(url)
   
-  var headers = copy($httpMethod, len("http"))
+  var headers = substr($httpMethod, len("http"))
   headers.add(" /" & r.path & r.query)
   headers.add(" HTTP/1.1\c\L")
   
diff --git a/lib/pure/httpserver.nim b/lib/pure/httpserver.nim
index f8368a631..eb5dd7d73 100755
--- a/lib/pure/httpserver.nim
+++ b/lib/pure/httpserver.nim
@@ -121,7 +121,7 @@ proc executeCgi(client: TSocket, path, query: string, meth: TRequestMethod) =
       if L.startsWith("content-length:"):
         var i = len("content-length:")
         while L[i] in Whitespace: inc(i)
-        contentLength = parseInt(copy(L, i))
+        contentLength = parseInt(substr(L, i))
 
     if contentLength < 0:
       badRequest(client)
@@ -165,7 +165,7 @@ proc acceptRequest(client: TSocket) =
   # extract path
   if q >= 0:
     # strip "?..." from path, this may be found in both POST and GET
-    path = "." & data[1].copy(0, q-1)
+    path = "." & data[1].substr(0, q-1)
   else:
     path = "." & data[1]
   # path starts with "/", by adding "." in front of it we serve files from cwd
@@ -173,7 +173,7 @@ proc acceptRequest(client: TSocket) =
   if cmpIgnoreCase(data[0], "GET") == 0:
     if q >= 0:
       cgi = true
-      query = data[1].copy(q+1)
+      query = data[1].substr(q+1)
   elif cmpIgnoreCase(data[0], "POST") == 0:
     cgi = true
     meth = reqPost
@@ -250,11 +250,11 @@ proc next*(s: var TServer) =
     if data[last] == '?' and query == 0: query = last
     inc(last)
   if query > 0:
-    s.query = data.copy(query+1, last-1)
-    s.path = data.copy(i, query-1)
+    s.query = data.substr(query+1, last-1)
+    s.path = data.substr(i, query-1)
   else:
     s.query = ""
-    s.path = data.copy(i, last-1)
+    s.path = data.substr(i, last-1)
 
 proc close*(s: TServer) =
   ## closes the server (and the socket the server uses).
diff --git a/lib/pure/json.nim b/lib/pure/json.nim
index adb7e5e8b..7b2707784 100755
--- a/lib/pure/json.nim
+++ b/lib/pure/json.nim
@@ -649,7 +649,8 @@ proc nl(s: var string, ml: bool) =
 
 proc escapeJson*(s: string): string = 
   ## Converts a string `s` to its JSON representation.
-  result = "\""
+  result = newStringOfCap(s.len + s.len shr 3)
+  result.add("\"")
   for x in runes(s):
     var r = int(x)
     if r >= 32 and r <= 127:
diff --git a/lib/pure/os.nim b/lib/pure/os.nim
index 7847fa332..2b2cb1ba7 100755
--- a/lib/pure/os.nim
+++ b/lib/pure/os.nim
@@ -330,7 +330,7 @@ proc JoinPath*(head, tail: string): string {.
     result = tail
   elif head[len(head)-1] in {DirSep, AltSep}:
     if tail[0] in {DirSep, AltSep}:
-      result = head & copy(tail, 1)
+      result = head & substr(tail, 1)
     else:
       result = head & tail
   else:
@@ -362,8 +362,8 @@ proc SplitPath*(path: string, head, tail: var string) {.noSideEffect,
       sepPos = i
       break
   if sepPos >= 0:
-    head = copy(path, 0, sepPos-1)
-    tail = copy(path, sepPos+1)
+    head = substr(path, 0, sepPos-1)
+    tail = substr(path, sepPos+1)
   else:
     head = ""
     tail = path # make a string copy here
@@ -388,8 +388,8 @@ proc SplitPath*(path: string): tuple[head, tail: string] {.
       sepPos = i
       break
   if sepPos >= 0:
-    result.head = copy(path, 0, sepPos-1)
-    result.tail = copy(path, sepPos+1)
+    result.head = substr(path, 0, sepPos-1)
+    result.tail = substr(path, sepPos+1)
   else:
     result.head = ""
     result.tail = path
@@ -412,7 +412,7 @@ proc parentDir*(path: string): string {.
       sepPos = i
       break
   if sepPos >= 0:
-    result = copy(path, 0, sepPos-1)
+    result = substr(path, 0, sepPos-1)
   else:
     result = path
 
@@ -462,9 +462,9 @@ proc splitFile*(path: string): tuple[dir, name, ext: string] {.
       elif path[i] in {dirsep, altsep}:
         sepPos = i
         break
-    result.dir = copy(path, 0, sepPos-1)
-    result.name = copy(path, sepPos+1, dotPos-1)
-    result.ext = copy(path, dotPos)
+    result.dir = substr(path, 0, sepPos-1)
+    result.name = substr(path, sepPos+1, dotPos-1)
+    result.ext = substr(path, dotPos)
 
 proc extractDir*(path: string): string {.noSideEffect, deprecated.} =
   ## Extracts the directory of a given path. This is almost the
@@ -507,8 +507,8 @@ proc SplitFilename*(filename: string, name, extension: var string) {.
   ## **Deprecated since version 0.8.2**: Use ``splitFile(filename)`` instead.
   var extPos = searchExtPos(filename)
   if extPos >= 0:
-    name = copy(filename, 0, extPos-1)
-    extension = copy(filename, extPos)
+    name = substr(filename, 0, extPos-1)
+    extension = substr(filename, extPos)
   else:
     name = filename # make a string copy here
     extension = ""
@@ -537,7 +537,7 @@ proc ChangeFileExt*(filename, ext: string): string {.
   ## of none such beast.)
   var extPos = searchExtPos(filename)
   if extPos < 0: result = filename & normExt(ext)
-  else: result = copy(filename, 0, extPos-1) & normExt(ext)
+  else: result = substr(filename, 0, extPos-1) & normExt(ext)
 
 proc addFileExt*(filename, ext: string): string {.
   noSideEffect, rtl, extern: "nos$1".} =
@@ -748,7 +748,7 @@ proc getEnv*(key: string): string =
   ## `existsEnv(key)`.
   var i = findEnvVar(key)
   if i >= 0:
-    return copy(environment[i], find(environment[i], '=')+1)
+    return substr(environment[i], find(environment[i], '=')+1)
   else:
     var env = cgetenv(key)
     if env == nil: return ""
@@ -788,7 +788,7 @@ iterator iterOverEnvironment*(): tuple[key, value: string] {.deprecated.} =
   getEnvVarsC()
   for i in 0..high(environment):
     var p = find(environment[i], '=')
-    yield (copy(environment[i], 0, p-1), copy(environment[i], p+1))
+    yield (substr(environment[i], 0, p-1), substr(environment[i], p+1))
 
 iterator envPairs*(): tuple[key, value: string] =
   ## Iterate over all `environments variables`:idx:. In the first component 
@@ -797,7 +797,7 @@ iterator envPairs*(): tuple[key, value: string] =
   getEnvVarsC()
   for i in 0..high(environment):
     var p = find(environment[i], '=')
-    yield (copy(environment[i], 0, p-1), copy(environment[i], p+1))
+    yield (substr(environment[i], 0, p-1), substr(environment[i], p+1))
 
 iterator walkFiles*(pattern: string): string =
   ## Iterate over all the files that match the `pattern`. On POSIX this uses
@@ -944,14 +944,14 @@ proc createDir*(dir: string) {.rtl, extern: "nos$1".} =
   ## fail if the path already exists because for most usages this does not 
   ## indicate an error.
   for i in 1.. dir.len-1:
-    if dir[i] in {dirsep, altsep}: rawCreateDir(copy(dir, 0, i-1))
+    if dir[i] in {dirsep, altsep}: rawCreateDir(substr(dir, 0, i-1))
   rawCreateDir(dir)
 
 proc copyDir*(source, dest: string) {.rtl, extern: "nos$1".} =
   ## Copies a directory from `source` to `dest`. If this fails, `EOS` is raised.
   createDir(dest)
   for kind, path in walkDir(source):
-    var noSource = path.copy(source.len()+1)
+    var noSource = path.substr(source.len()+1)
     case kind
     of pcFile:
       copyFile(path, dest / noSource)
diff --git a/lib/pure/parseopt.nim b/lib/pure/parseopt.nim
index 1b09934f2..c4625c161 100755
--- a/lib/pure/parseopt.nim
+++ b/lib/pure/parseopt.nim
@@ -126,7 +126,7 @@ proc next*(p: var TOptParser) {.
 proc cmdLineRest*(p: TOptParser): string {.
   rtl, extern: "npo$1".} = 
   ## retrieves the rest of the command line that has not been parsed yet.
-  result = strip(copy(p.cmd, p.pos, len(p.cmd) - 1)) 
+  result = strip(substr(p.cmd, p.pos, len(p.cmd) - 1)) 
 
 proc getRestOfCommandLine*(p: TOptParser): string {.deprecated.} = 
   ## **Deprecated since version 0.8.2**: Use `cmdLineRest` instead.
diff --git a/lib/pure/parseutils.nim b/lib/pure/parseutils.nim
index 90bb5d79f..a7776bd5f 100755
--- a/lib/pure/parseutils.nim
+++ b/lib/pure/parseutils.nim
@@ -73,7 +73,7 @@ proc parseIdent*(s: string, ident: var string, start = 0): int =
   if s[i] in IdentStartChars:
     inc(i)
     while s[i] in IdentChars: inc(i)
-    ident = copy(s, start, i-1)
+    ident = substr(s, start, i-1)
     result = i-start
 
 proc parseToken*(s: string, token: var string, validChars: set[char],
@@ -86,7 +86,7 @@ proc parseToken*(s: string, token: var string, validChars: set[char],
   var i = start
   while s[i] in validChars: inc(i)
   result = i-start
-  token = copy(s, start, i-1)
+  token = substr(s, start, i-1)
 
 proc skipWhitespace*(s: string, start = 0): int {.inline.} =
   ## skips the whitespace starting at ``s[start]``. Returns the number of
@@ -120,7 +120,7 @@ proc parseUntil*(s: string, token: var string, until: set[char],
   var i = start
   while s[i] notin until: inc(i)
   result = i-start
-  token = copy(s, start, i-1)
+  token = substr(s, start, i-1)
 
 proc parseWhile*(s: string, token: var string, validChars: set[char],
                  start = 0): int {.inline.} =
@@ -130,7 +130,7 @@ proc parseWhile*(s: string, token: var string, validChars: set[char],
   var i = start
   while s[i] in validChars: inc(i)
   result = i-start
-  token = copy(s, start, i-1)
+  token = substr(s, start, i-1)
 
 {.push overflowChecks: on.}
 # this must be compiled with overflow checking turned on:
diff --git a/lib/pure/pegs.nim b/lib/pure/pegs.nim
index 9b2606b33..988e510e3 100755
--- a/lib/pure/pegs.nim
+++ b/lib/pure/pegs.nim
@@ -736,7 +736,7 @@ proc rawMatch*(s: string, p: TPeg, start: int, c: var TCaptures): int {.
     var (a, b) = c.matches[p.index]
     var n: TPeg
     n.kind = succ(pkTerminal, ord(p.kind)-ord(pkBackRef)) 
-    n.term = s.copy(a, b)
+    n.term = s.substr(a, b)
     result = rawMatch(s, n, start, c)
   of pkStartAnchor:
     if c.origStart == start: result = 0
@@ -754,7 +754,7 @@ proc match*(s: string, pattern: TPeg, matches: var openarray[string],
   result = rawMatch(s, pattern, start, c) == len(s) - start
   if result:
     for i in 0..c.ml-1:
-      matches[i] = copy(s, c.matches[i][0], c.matches[i][1])
+      matches[i] = substr(s, c.matches[i][0], c.matches[i][1])
 
 proc match*(s: string, pattern: TPeg, 
             start = 0): bool {.nosideEffect, rtl, extern: "npegs$1".} =
@@ -774,7 +774,7 @@ proc matchLen*(s: string, pattern: TPeg, matches: var openarray[string],
   result = rawMatch(s, pattern, start, c)
   if result >= 0:
     for i in 0..c.ml-1:
-      matches[i] = copy(s, c.matches[i][0], c.matches[i][1])
+      matches[i] = substr(s, c.matches[i][0], c.matches[i][1])
 
 proc matchLen*(s: string, pattern: TPeg, 
                start = 0): int {.nosideEffect, rtl, extern: "npegs$1".} =
@@ -903,7 +903,7 @@ proc replacef*(s: string, sub: TPeg, by: string): string {.
     else:
       addf(result, by, caps)
       inc(i, x)
-  add(result, copy(s, i))
+  add(result, substr(s, i))
 
 proc replace*(s: string, sub: TPeg, by = ""): string {.
   nosideEffect, rtl, extern: "npegs$1".} =
@@ -920,7 +920,7 @@ proc replace*(s: string, sub: TPeg, by = ""): string {.
     else:
       addf(result, by, caps)
       inc(i, x)
-  add(result, copy(s, i))
+  add(result, substr(s, i))
   
 proc parallelReplace*(s: string, subs: openArray[
                       tuple[pattern: TPeg, repl: string]]): string {.
@@ -941,7 +941,7 @@ proc parallelReplace*(s: string, subs: openArray[
       add(result, s[i])
       inc(i)
   # copy the rest:
-  add(result, copy(s, i))  
+  add(result, substr(s, i))  
   
 proc transformFile*(infile, outfile: string,
                     subs: openArray[tuple[pattern: TPeg, repl: string]]) {.
@@ -990,7 +990,7 @@ iterator split*(s: string, sep: TPeg): string =
       x = matchLen(s, sep, last)
       if x > 0: break
     if first < last:
-      yield copy(s, first, last-1)
+      yield substr(s, first, last-1)
 
 proc split*(s: string, sep: TPeg): seq[string] {.
   nosideEffect, rtl, extern: "npegs$1".} =
@@ -1688,7 +1688,7 @@ when isMainModule:
   assert rawMatch(s, expr.rule, 0, c) == len(s)
   var a = ""
   for i in 0..c.ml-1:
-    a.add(copy(s, c.matches[i][0], c.matches[i][1]))
+    a.add(substr(s, c.matches[i][0], c.matches[i][1]))
   assert a == "abcdef"
   #echo expr.rule
 
diff --git a/lib/pure/regexprs.nim b/lib/pure/regexprs.nim
index fb9601c18..3524aac0a 100755
--- a/lib/pure/regexprs.nim
+++ b/lib/pure/regexprs.nim
@@ -84,7 +84,7 @@ proc matchOrFind(s: string, pattern: PPcre, matches: var openarray[string],
     var
       a = rawMatches[i * 2]
       b = rawMatches[i * 2 + 1]
-    if a >= 0'i32: matches[i] = copy(s, a, int(b)-1)
+    if a >= 0'i32: matches[i] = substr(s, a, int(b)-1)
     else: matches[i] = ""
   return res
 
diff --git a/lib/pure/ropes.nim b/lib/pure/ropes.nim
index 52406d4d1..69737576f 100755
--- a/lib/pure/ropes.nim
+++ b/lib/pure/ropes.nim
@@ -287,7 +287,7 @@ when false:
         if frmt[i] != '$': inc(i)
         else: break 
       if i - 1 >= start: 
-        add(result, copy(frmt, start, i-1))
+        add(result, substr(frmt, start, i-1))
   
 proc `%`*(frmt: string, args: openarray[PRope]): PRope {. 
   rtl, extern: "nroFormat".} =
@@ -330,7 +330,7 @@ proc `%`*(frmt: string, args: openarray[PRope]): PRope {.
       if frmt[i] != '$': inc(i)
       else: break 
     if i - 1 >= start: 
-      add(result, copy(frmt, start, i - 1))
+      add(result, substr(frmt, start, i - 1))
 
 proc addf*(c: var PRope, frmt: string, args: openarray[PRope]) {.
   rtl, extern: "nro$1".} =
diff --git a/lib/pure/strtabs.nim b/lib/pure/strtabs.nim
index 78f489615..2028daa50 100755
--- a/lib/pure/strtabs.nim
+++ b/lib/pure/strtabs.nim
@@ -181,12 +181,12 @@ proc `%`*(f: string, t: PStringTable, flags: set[TFormatFlag] = {}): string {.
       of '{':
         var j = i + 1
         while j < f.len and f[j] != '}': inc(j)
-        add(result, getValue(t, flags, copy(f, i+2, j-1)))
+        add(result, getValue(t, flags, substr(f, i+2, j-1)))
         i = j + 1
       of 'a'..'z', 'A'..'Z', '\x80'..'\xFF', '_':
         var j = i + 1
         while j < f.len and f[j] in PatternChars: inc(j)
-        add(result, getValue(t, flags, copy(f, i+1, j-1)))
+        add(result, getValue(t, flags, substr(f, i+1, j-1)))
         i = j
       else:
         add(result, f[i])
diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim
index 382eece7b..7ed224b67 100755
--- a/lib/pure/strutils.nim
+++ b/lib/pure/strutils.nim
@@ -83,18 +83,22 @@ proc capitalize*(s: string): string {.noSideEffect, procvar,
   rtl, extern: "nsuCapitalize".} =

   ## Converts the first character of `s` into upper case.

   ## This works only for the letters a-z.

-  result = toUpper(s[0]) & copy(s, 1)

+  result = toUpper(s[0]) & substr(s, 1)

 

 proc normalize*(s: string): string {.noSideEffect, procvar,

   rtl, extern: "nsuNormalize".} =

   ## Normalizes the string `s`. That means to convert it to lower case and

   ## remove any '_'. This is needed for Nimrod identifiers for example.

-  result = ""

+  result = newString(s.len)
+  var j = 0

   for i in 0..len(s) - 1:

     if s[i] in {'A'..'Z'}:

-      add result, Chr(Ord(s[i]) + (Ord('a') - Ord('A')))

+      result[j] = Chr(Ord(s[i]) + (Ord('a') - Ord('A')))
+      inc j

     elif s[i] != '_':

-      add result, s[i]

+      result[j] = s[i]
+      inc j
+  if j != s.len: setLen(result, j)

 

 proc cmpIgnoreCase*(a, b: string): int {.noSideEffect,

   rtl, extern: "nsuCmpIgnoreCase", procvar.} =

@@ -171,14 +175,14 @@ proc addf*(s: var string, formatstr: string, a: openarray[string]) {.
       of '{':

         var j = i+1

         while formatstr[j] notin {'\0', '}'}: inc(j)

-        var x = findNormalized(copy(formatstr, i+2, j-1), a)

+        var x = findNormalized(substr(formatstr, i+2, j-1), a)

         if x >= 0 and x < high(a): add s, a[x+1]

         else: raise newException(EInvalidValue, "invalid format string")

         i = j+1

       of 'a'..'z', 'A'..'Z', '\128'..'\255', '_':

         var j = i+1

         while formatstr[j] in PatternChars: inc(j)

-        var x = findNormalized(copy(formatstr, i+1, j-1), a)

+        var x = findNormalized(substr(formatstr, i+1, j-1), a)

         if x >= 0 and x < high(a): add s, a[x+1]

         else: raise newException(EInvalidValue, "invalid format string")

         i = j

@@ -226,13 +230,14 @@ proc `%` *(formatstr: string, a: openarray[string]): string {.noSideEffect,
   ##

   ## The variables are compared with `cmpIgnoreStyle`. `EInvalidValue` is

   ## raised if an ill-formed format string has been passed to the `%` operator.

-  result = ""

+  result = newStringOfCap(formatstr.len + a.len shl 4)

   addf(result, formatstr, a)

 

 proc `%` *(formatstr, a: string): string {.noSideEffect, 

   rtl, extern: "nsuFormatSingleElem".} =

   ## This is the same as ``formatstr % [a]``.

-  return formatstr % [a]

+  result = newStringOfCap(formatstr.len + a.len)

+  addf(result, formatstr, [a])

 

 proc strip*(s: string, leading = true, trailing = true): string {.noSideEffect,

   rtl, extern: "nsuStrip".} =

@@ -248,7 +253,7 @@ proc strip*(s: string, leading = true, trailing = true): string {.noSideEffect,
     while s[first] in chars: inc(first)

   if trailing:

     while last >= 0 and s[last] in chars: dec(last)

-  result = copy(s, first, last)

+  result = substr(s, first, last)

 

 proc toOctal*(c: char): string {.noSideEffect, rtl, extern: "nsuToOctal".} =

   ## Converts a character `c` to its octal representation. The resulting

@@ -288,7 +293,7 @@ iterator split*(s: string, seps: set[char] = Whitespace): string =
     var first = last

     while last < len(s) and s[last] not_in seps: inc(last) # BUGFIX!

     if first <= last-1:

-      yield copy(s, first, last-1)

+      yield substr(s, first, last-1)

 

 iterator split*(s: string, sep: char): string =

   ## Splits the string `s` into substrings.

@@ -321,7 +326,7 @@ iterator split*(s: string, sep: char): string =
     while last <= len(s):

       var first = last

       while last < len(s) and s[last] != sep: inc(last)

-      yield copy(s, first, last-1)

+      yield substr(s, first, last-1)

       inc(last)

 

 iterator splitLines*(s: string): string =

@@ -349,7 +354,7 @@ iterator splitLines*(s: string): string =
   var last = 0

   while true:

     while s[last] notin {'\0', '\c', '\l'}: inc(last)

-    yield copy(s, first, last-1)

+    yield substr(s, first, last-1)

     # skip newlines:

     if s[last] == '\l': inc(last)

     elif s[last] == '\c':

@@ -499,7 +504,7 @@ iterator tokenize*(s: string, seps: set[char] = Whitespace): tuple[
     var isSep = s[j] in seps

     while j < s.len and (s[j] in seps) == isSep: inc(j)

     if j > i:

-      yield (copy(s, i, j-1), isSep)

+      yield (substr(s, i, j-1), isSep)

     else:

       break

     i = j

@@ -510,19 +515,19 @@ proc wordWrap*(s: string, maxLineWidth = 80,
                newLine = "\n"): string {.

                noSideEffect, rtl, extern: "nsuWordWrap".} = 

   ## word wraps `s`.

-  result = ""

+  result = newStringOfCap(s.len + s.len shr 6)

   var SpaceLeft = maxLineWidth

   for word, isSep in tokenize(s, seps):

     if len(word) > SpaceLeft:

       if splitLongWords and len(word) > maxLineWidth:

-        result.add(copy(word, 0, spaceLeft-1))

+        result.add(substr(word, 0, spaceLeft-1))

         var w = spaceLeft+1

         var wordLeft = len(word) - spaceLeft

         while wordLeft > 0: 

           result.add(newLine)

           var L = min(maxLineWidth, wordLeft)

           SpaceLeft = maxLineWidth - L

-          result.add(copy(word, w, w+L-1))

+          result.add(substr(word, w, w+L-1))

           inc(w, L)

           dec(wordLeft, L)

       else:

@@ -700,11 +705,11 @@ proc replace*(s, sub: string, by = ""): string {.noSideEffect,
   while true:

     var j = findAux(s, sub, i, a)

     if j < 0: break

-    add result, copy(s, i, j - 1)

+    add result, substr(s, i, j - 1)

     add result, by

     i = j + len(sub)

   # copy the rest:

-  add result, copy(s, i)

+  add result, substr(s, i)

 

 proc replace*(s: string, sub, by: char): string {.noSideEffect,

   rtl, extern: "nsuReplaceChar".} =

@@ -804,7 +809,8 @@ proc escape*(s: string, prefix = "\"", suffix = "\""): string {.noSideEffect,
   ## The procedure has been designed so that its output is usable for many

   ## different common syntaxes. The resulting string is prefixed with

   ## `prefix` and suffixed with `suffix`. Both may be empty strings.

-  result = prefix

+  result = newStringOfCap(s.len + s.len shr 2)
+  result.add(prefix)

   for c in items(s):

     case c

     of '\0'..'\31', '\128'..'\255':

@@ -837,7 +843,7 @@ proc validEmailAddress*(s: string): bool {.noSideEffect,
   while s[i] in {'0'..'9', 'a'..'z', '-', '.'}: inc(i) 

   if s[i] != '\0': return false

   

-  var x = copy(s, j+1)

+  var x = substr(s, j+1)

   if len(x) == 2 and x[0] in Letters and x[1] in Letters: return true

   case toLower(x)

   of "com", "org", "net", "gov", "mil", "biz", "info", "mobi", "name",

diff --git a/lib/pure/xmldom.nim b/lib/pure/xmldom.nim
index bf031fb88..923fc9e18 100755
--- a/lib/pure/xmldom.nim
+++ b/lib/pure/xmldom.nim
@@ -1021,9 +1021,9 @@ proc splitData*(TextNode: PText, offset: int): PText =
   if offset > TextNode.data.len():
     raise newException(EIndexSizeErr, "Index out of bounds")
   
-  var left: string = TextNode.data.copy(0, offset)
+  var left: string = TextNode.data.substr(0, offset)
   TextNode.data = left
-  var right: string = TextNode.data.copy(offset, TextNode.data.len())
+  var right: string = TextNode.data.substr(offset, TextNode.data.len())
   
   if TextNode.FParentNode != nil:
     for i in low(TextNode.FParentNode.childNodes)..high(TextNode.FParentNode.childNodes):