Source Edit

This module contains support for a rope data type. Ropes can represent very long strings efficiently; in particular, concatenation is done in O(1) instead of O(n). They are essentially concatenation trees that are only flattened when converting to a native Nim string. The empty string is represented by nil. Ropes are immutable and subtrees can be shared without copying. Leaves can be cached for better memory efficiency at the cost of runtime efficiency.

Imports

streams

Types

  1. Rope {.acyclic.} = ref object

A rope data type. The empty rope is represented by nil. Source Edit

Procs

  1. proc `$`(r: Rope): string {....gcsafe, extern: "nroToString", raises: [], tags: [],
  2. forbids: [].}

Converts a rope back to a string. Source Edit

  1. proc `%`(frmt: string; args: openArray[Rope]): Rope {....gcsafe,
  2. extern: "nroFormat", raises: [ValueError], tags: [], forbids: [].}

% substitution operator for ropes. Does not support the $identifier nor ${identifier} notations.

Example:

  1. let r1 = "$1 $2 $3" % [rope("Nim"), rope("is"), rope("a great language")]
  2. doAssert $r1 == "Nim is a great language"
  3. let r2 = "$# $# $#" % [rope("Nim"), rope("is"), rope("a great language")]
  4. doAssert $r2 == "Nim is a great language"
  5. let r3 = "${1} ${2} ${3}" % [rope("Nim"), rope("is"), rope("a great language")]
  6. doAssert $r3 == "Nim is a great language"

Source Edit

  1. proc `&`(a, b: Rope): Rope {....gcsafe, extern: "nroConcRopeRope", raises: [],
  2. tags: [], forbids: [].}

The concatenation operator for ropes.

Example:

  1. let r = rope("Hello, ") & rope("Nim!")
  2. doAssert $r == "Hello, Nim!"

Source Edit

  1. proc `&`(a: openArray[Rope]): Rope {....gcsafe, extern: "nroConcOpenArray",
  2. raises: [], tags: [], forbids: [].}

The concatenation operator for an openArray of ropes.

Example:

  1. let r = &[rope("Hello, "), rope("Nim"), rope("!")]
  2. doAssert $r == "Hello, Nim!"

Source Edit

  1. proc `&`(a: Rope; b: string): Rope {....gcsafe, extern: "nroConcRopeStr",
  2. raises: [], tags: [], forbids: [].}

The concatenation operator for ropes.

Example:

  1. let r = rope("Hello, ") & "Nim!"
  2. doAssert $r == "Hello, Nim!"

Source Edit

  1. proc `&`(a: string; b: Rope): Rope {....gcsafe, extern: "nroConcStrRope",
  2. raises: [], tags: [], forbids: [].}

The concatenation operator for ropes.

Example:

  1. let r = "Hello, " & rope("Nim!")
  2. doAssert $r == "Hello, Nim!"

Source Edit

  1. proc `[]`(r: Rope; i: int): char {....gcsafe, extern: "nroCharAt", raises: [],
  2. tags: [], forbids: [].}

Returns the character at position i in the rope r. This is quite expensive! Worst-case: O(n). If i >= r.len or i < 0, \0 is returned.

Example:

  1. let r = rope("Hello, Nim!")
  2. doAssert r[0] == 'H'
  3. doAssert r[7] == 'N'
  4. doAssert r[22] == '\0'

Source Edit

  1. proc add(a: var Rope; b: Rope) {....gcsafe, extern: "nro$1Rope", raises: [],
  2. tags: [], forbids: [].}

Adds b to the rope a.

Example:

  1. var r = rope("Hello, ")
  2. r.add(rope("Nim!"))
  3. doAssert $r == "Hello, Nim!"

Source Edit

  1. proc add(a: var Rope; b: string) {....gcsafe, extern: "nro$1Str", raises: [],
  2. tags: [], forbids: [].}

Adds b to the rope a.

Example:

  1. var r = rope("Hello, ")
  2. r.add("Nim!")
  3. doAssert $r == "Hello, Nim!"

Source Edit

  1. proc addf(c: var Rope; frmt: string; args: openArray[Rope]) {....gcsafe,
  2. extern: "nro$1", raises: [ValueError], tags: [], forbids: [].}

Shortcut for add(c, frmt % args).

Example:

  1. var r = rope("Dash: ")
  2. r.addf "$1 $2 $3", [rope("Nim"), rope("is"), rope("a great language")]
  3. doAssert $r == "Dash: Nim is a great language"

Source Edit

  1. proc disableCache() {....gcsafe, extern: "nro$1", raises: [], tags: [], forbids: [].}

The cache is discarded and disabled. The GC will reuse its used memory. Source Edit

  1. proc enableCache() {....gcsafe, extern: "nro$1", raises: [], tags: [], forbids: [].}

Enables the caching of leaves. This reduces the memory footprint at the cost of runtime efficiency. Source Edit

  1. proc equalsFile(r: Rope; f: File): bool {....gcsafe, extern: "nro$1File",
  2. raises: [IOError], tags: [ReadIOEffect], forbids: [].}

Returns true if the contents of the file f equal r. Source Edit

  1. proc equalsFile(r: Rope; filename: string): bool {....gcsafe, extern: "nro$1Str",
  2. raises: [IOError], tags: [ReadIOEffect], forbids: [].}

Returns true if the contents of the file f equal r. If f does not exist, false is returned. Source Edit

  1. proc len(a: Rope): int {....gcsafe, extern: "nro$1", raises: [], tags: [],
  2. forbids: [].}

The rope’s length. Source Edit

  1. proc rope(f: BiggestFloat): Rope {....gcsafe, extern: "nro$1BiggestFloat",
  2. raises: [], tags: [], forbids: [].}

Converts a float to a rope.

Example:

  1. let r = rope(4.29)
  2. doAssert $r == "4.29"

Source Edit

  1. proc rope(i: BiggestInt): Rope {....gcsafe, extern: "nro$1BiggestInt", raises: [],
  2. tags: [], forbids: [].}

Converts an int to a rope.

Example:

  1. let r = rope(429)
  2. doAssert $r == "429"

Source Edit

  1. proc rope(s: string = ""): Rope {....gcsafe, extern: "nro$1Str", raises: [],
  2. tags: [], forbids: [].}

Converts a string to a rope.

Example:

  1. let r = rope("I'm a rope")
  2. doAssert $r == "I'm a rope"

Source Edit

  1. proc write(f: File; r: Rope) {....gcsafe, extern: "nro$1", raises: [IOError],
  2. tags: [WriteIOEffect], forbids: [].}

Writes a rope to a file. Source Edit

  1. proc write(s: Stream; r: Rope) {....gcsafe, extern: "nroWriteStream",
  2. raises: [IOError, OSError],
  3. tags: [WriteIOEffect], forbids: [].}

Writes a rope to a stream. Source Edit

Iterators

  1. iterator items(r: Rope): char {....raises: [], tags: [], forbids: [].}

Iterates over any character in the rope r. Source Edit

  1. iterator leaves(r: Rope): string {....raises: [], tags: [], forbids: [].}

Iterates over any leaf string in the rope r.

Example:

  1. let r = rope("Hello") & rope(", Nim!")
  2. let s = ["Hello", ", Nim!"]
  3. var index = 0
  4. for leave in r.leaves:
  5. doAssert leave == s[index]
  6. inc(index)

Source Edit