<< Prev | - Up - |
The module VirtualString
contains procedures operating on virtual strings. Virtual strings are designed as a convenient way to combine strings, byte strings, atoms, integers and floats to compound strings without explicit concatenation and conversion.
IsVirtualString
{VirtualString.is
+X
?B
}
tests whether X
is a virtual string. Virtual strings are defined recursively as the set of all integers, floats, atoms, strings, byte strings, and tuples with label '#'
whose subtrees are virtual strings.
toString
{VirtualString.toString
+V
?S
}
converts a virtual string V
to a string S
.
The transformation is straightforward: Atoms (except nil
and '#'
), integers, floats and byte strings are transformed into strings using Atom.toString
, Int.toString
, Float.toString
, and ByteString.toString
respectively, where in numbers -
is used instead of ~
. A tuple with label '#'
is transformed by concatenation of the transformed subtrees. Note that both nil
and '#'
denote the empty string.
The following relation holds for all virtual strings V1
and V2
:
{VirtualString.toString V1#V2}
= {Append
{VirtualString.toString V1}
{VirtualString.toString V2}}
Thus, VirtualString.toString
maps #
homomorphically to Append
.
toAtom
{VirtualString.toAtom
+V
?A
}
converts a virtual string V
to an atom A
.
This procedure can be defined as:
fun {VirtualString.toAtom V}
{String.toAtom {VirtualString.toString V}}
end
toByteString
{VirtualString.toByteString
+V
?ByteString
}
converts a virtual string V
to a byte string ByteString
.
This procedure is a synonym of ByteString.make
(which see).
length
{VirtualString.length
+V
?I
}
returns the length of a virtual string in characters. Can be defined as:
{Length {VirtualString.toString
V
}
I
}
changeSign
{VirtualString.changeSign
+V1
X
?V2
}
returns a virtual string derived from V1
where all occurrences of the unary minus sign for integers and floats are replaced by X
.
<< Prev | - Up - |