all repos — min @ 1b5b31f4f2ced89983f88e60eeea212026b66133

A small but practical concatenative programming language.

core/baseutils.nim

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
proc reverse*[T](xs: openarray[T]): seq[T] =
  result = newSeq[T](xs.len)
  for i, x in xs:
    result[result.len-i-1] = x 


when defined(mini):
  import
    strutils
  
  proc parentDirEx*(s: string): string =
    let fslash = s.rfind("/")
    let bslash = s.rfind("\\")
    var dirEnd = fslash-1
    if dirEnd < 0:
      dirEnd = bslash-1
    if dirEnd < 0:
      dirEnd = s.len-1
    if dirEnd < 0:
      return s
    return s[0..dirEnd]
    
  proc escapeEx*(s: string, unquoted = false): string =
    for c in s:
      case c
      of '\L': result.add("\\n")
      of '\b': result.add("\\b")
      of '\f': result.add("\\f")
      of '\t': result.add("\\t")
      of '\v': result.add("\\u000b")
      of '\r': result.add("\\r")
      of '"': result.add("\\\"")
      of '\0'..'\7': result.add("\\u000" & $ord(c))
      of '\14'..'\31': result.add("\\u00" & toHex(ord(c), 2))
      of '\\': result.add("\\\\")
      else: result.add(c)
    if unquoted:
      return result
    return "\"" & result & "\""
    
else:
  import os, json
  
  proc parentDirEx*(s: string): string =
    return s.parentDir
    
  proc escapeEx*(s: string, unquoted = false): string =
    if unquoted:
      return s.escapeJsonUnquoted
    return s.escapeJson