discard """ batchable: false """ # # # Nim's Runtime Library # (c) Copyright 2018 Nim Contributors # # See the file "copying.txt", included in this # distribution, for details about the copyright. # # This is the Nim hot code reloading run-time for the native targets. # # This minimal dynamic library is not subject to reloading when the # `hotCodeReloading` build mode is enabled. It's responsible for providing # a permanent memory location for all globals and procs within a program # and orchestrating the reloading. For globals, this is easily achieved # by storing them on the heap. For procs, we produce on the fly simple # trampolines that can be dynamically overwritten to jump to a different # target. In the host program, all globals and procs are first registered # here with `hcrRegisterGlobal` and `hcrRegisterProc` and then the # returned permanent locations are used in every reference to these symbols # onwards. # # Detailed description: # # When code is compiled with the hotCodeReloading option for native targets # a couple of things happen for all modules in a project: # - the useNimRtl option is forced (including when building the HCR runtime too) # - all modules of a target get built into separate shared libraries # - the smallest granularity of reloads is modules # - for each .c (or .cpp) in the corresponding nimcache folder of the project # a shared object is built with the name of the source file + DLL extension # - only the main module produces whatever the original project type intends # (again in nimcache) and is then copied to its original destination # - linking is done in parallel - just like compilation # - function calls to functions from the same project go through function pointers: # - with a few exceptions - see the nonReloadable pragma # - the forward declarations of the original functions become function # pointers as static globals with the same names # - the original function definitions get suffixed with _actual # - the function pointers get initialized with the address of the corresponding # function in the DatInit of their module through a call to either hcrRegisterProc # or hcrGetProc. When being registered, the _actual address is passed to # hcrRegisterProc and a permanent location is returned and assigned to the pointer. # This way the implementation (_actual) can change but the address for it # will be the same - this works by just updating a jump instruction (trampoline). # For functions from other modules hcrGetProc is used (after they are registered). # - globals are initialized only once and their state is preserved # - including locals with the {.global.} pragma # - their definitions are changed into pointer definitions which are initialized # in the DatInit() of their module with calls to hcrRegisterGlobal (supplying the # size of the type that this HCR runtime should allocate) and a bool is returned # which when true triggers the initialization code for the global (only once). # Globals from other modules: a global pointer coupled with a hcrGetGlobal call. # - globals which have already been initialized cannot have their values changed # by changing their initialization - use a handler or some other mechanism # - new globals can be introduced when reloading # - top-level code (global scope) is executed only once - at the first module load # - the runtime knows every symbol's module owner (globals and procs) # - both the RTL and HCR shared libraries need to be near the program for execution # - same folder, in the PATH or LD_LIBRARY_PATH env var, etc (depending on OS) # - the main module is responsible for initializing the HCR runtime # - the main module loads the RTL and HCR shared objects # - after that a call to hcrInit() is done in the main module which triggers # the loading of all modules the main one imports, and doing that for the # dependencies of each module recursively. Basically a DFS traversal. # - then initialization takes place with several passes over all modules: # - HcrInit - initializes the pointers for HCR procs such as hcrRegisterProc # - HcrCreateTypeInfos - creates globals which will be referenced in the next pass # - DatInit - usual dat init + register/get procs and get globals # - Init - it does the following multiplexed operations: # - register globals (if already registered - then just retrieve pointer) # - execute top level scope (only if loaded for the first time) # - when modules are loaded the originally built shared libraries get copied in # the same folder and the copies are loaded instead of the original files # - a module import tree is built in the runtime (and maintained when reloading) # - hcrPerformCodeReload # - named `performCodeReload`, requires the hotcodereloading module # - explicitly called by the user - the current active callstack shouldn't contain # any functions which are defined in modules that will be reloaded (or crash!). # The reason is that old dynamic libraries get unloaded. # Example: # if A is the main module and it imports B, then only B is reloadable and only # if when calling hcrPerformCodeReload there is no function defined in B in the # current active callstack at the point of the call (it has to be done from A) # - for reloading to take place the user has to have rebuilt parts of the application # without changes affecting the main module in any way - it shouldn't be rebuilt. # - to determine what needs to be reloaded the runtime starts traversing the import # tree from the root and checks the t
#
#
#            Nim's Runtime Library
#        (c) Copyright 2016 Joey Payne
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## This module contains various string utility routines that are uncommonly
## used in comparison to `strutils <strutils.html>`_.

import strutils

{.deadCodeElim: on.}

proc expandTabs*(s: string, tabSize: int = 8): string {.noSideEffect,
  procvar.} =
  ## Expand tab characters in `s` by `tabSize` spaces

  result = newStringOfCap(s.len + s.len shr 2)
  var pos = 0

  template addSpaces(n) =
    for j in 0 ..< n:
      result.add(' ')
      pos += 1

  for i in 0 ..< len(s):
    let c = s[i]
    if c == '\t':
      let
        denominator = if tabSize > 0: tabSize else: 1
        numSpaces = tabSize - pos mod denominator

      addSpaces(numSpaces)
    else:
      result.add(c)
      pos += 1
    if c == '\l':
      pos = 0

proc partition*(s: string, sep: string,
                right: bool = false): (string, string, string)
                {.noSideEffect, procvar.} =
  ## Split the string at the first or last occurrence of `sep` into a 3-tuple
  ##
  ## Returns a 3 string tuple of (beforeSep, `sep`, afterSep) or
  ## (`s`, "", "") if `sep` is not found and `right` is false or
  ## ("", "", `s`) if `sep` is not found and `right` is true
  let position = if right: s.rfind(sep) else: s.find(sep)
  if position != -1:
    return (s[0 ..< position], sep, s[position + sep.len ..< s.len])
  return if right: ("", "", s) else: (s, "", "")

proc rpartition*(s: string, sep: string): (string, string, string)
                {.noSideEffect, procvar.} =
  ## Split the string at the last occurrence of `sep` into a 3-tuple
  ##
  ## Returns a 3 string tuple of (beforeSep, `sep`, afterSep) or
  ## ("", "", `s`) if `sep` is not found
  return partition(s, sep, right = true)

when isMainModule:
  doAssert expandTabs("\t", 4) == "    "
  doAssert expandTabs("\tfoo\t", 4) == "    foo "
  doAssert expandTabs("\tfoo\tbar", 4) == "    foo bar"
  doAssert expandTabs("\tfoo\tbar\t", 4) == "    foo bar "
  doAssert expandTabs("", 4) == ""
  doAssert expandTabs("", 0) == ""
  doAssert expandTabs("\t\t\t", 0) == ""

  doAssert partition("foo:bar", ":") == ("foo", ":", "bar")
  doAssert partition("foobarbar", "bar") == ("foo", "bar", "bar")
  doAssert partition("foobarbar", "bank") == ("foobarbar", "", "")
  doAssert partition("foobarbar", "foo") == ("", "foo", "barbar")
  doAssert partition("foofoobar", "bar") == ("foofoo", "bar", "")

  doAssert rpartition("foo:bar", ":") == ("foo&quo