trick/common

    Dark Mode
Search:
Group by:

↩ back to overview

Types

View[T] = object
  len: int
  data: ptr UncheckedArray[T]

A simpler version of View from zielmicha/collections.nim

Allows you to treat arbitrary memory like an array of some type, without copying.

Not GC safe! Do not attempt to use this after the original data has gone out of scope or otherwise been deallocated.

Procs

proc `[]`[T](a: View[T]; i: int): var T
Retrieve an element from the View by index.
proc `[]=`[T](a: View[T]; i: int; val: T)
Assign an element in the View by index.
proc viewSeq[T](s: var seq[T]): View[T]
Interpret a sequence as a View of the same type.
proc viewSeqAs[A, B](s: var seq[B]): View[A]
Interpret a sequence of some type as a View of some other type.
proc viewBytesAs[T](data: var string): View[T]
Interpret a string of bytes as a View of a given type. This may be handy for working with data of a known binary format.
proc toSeq[T](a: View[T]): seq[T]
Copy all the elements of a View into a new sequence.
proc toBytes[T](a: View[T]): string
Returns a new string containing the raw data of a View.
proc toBytes[T](s: seq[T]): string
Convert a sequence to a string of bytes.
proc makeCString(data: string): Rope {...}{.raises: [], tags: [].}

Convert binary data into an escaped C string literal.

This is a utility function borrowed from the Nim compiler.

proc fileToVarName(name: string; firstUpper = false): string {...}{.raises: [],
    tags: [].}

Funcs

func toCamelCase(str: string; firstUpper = false): string {...}{.raises: [], tags: [].}

Convert a string from snake_case to camelCase.

echo "foo_bar".toCamelCase()   # fooBar

If first is true, the first character will be capitalized.

echo "foo_bar".toCamelCase(true)   # FooBar

Note: Uppercase characters in the input will not be changed. A name in all-caps should first be converted to lowercase like so:

echo "SFX_JUMP".toLowerAscii().toCamelCase() == "sfxJump"

Iterators

iterator items[T](a: View[T]): T
Iterate over the content of a View.