Table of Contents
1. cvn
Converts a string to a name object.
1.2. Stack Effects
| Level | Object |
|---|---|
0 |
|
| Level | Object |
|---|---|
0 |
|
1.3. Description
cvn (convert to name) converts the string operand to a name object that is lexically identical to the string.
The resulting name: * Has the same executable/literal attribute as the string * Is interned in the name table * Can be used as a dictionary key
1.5. Examples
Basic conversion
(abc) cvn % Result: /abc
Executable name
(abc) cvx cvn % Result: abc (executable)
Dynamic name generation
(font) 10 20 string cvs
exch dup length string
dup 0 4 3 index putinterval
dup 4 3 index putinterval
cvn
% Result: /font10
1.6. Common Use Cases
1.6.1. Dynamic Dictionary Keys
/prefix (item) def
0 1 9 {
20 string cvs
prefix exch concatstrings
cvn % Convert to name for dict key
% Use as dictionary key
} for
1.7. Common Pitfalls
| Name Length Limit - Names have implementation-dependent maximum length (typically 127 characters). |
very-long-string cvn % May cause limitcheck
| Names Are Interned - All identical name objects share the same entry in the name table, consuming VM. |
% Each unique name uses VM
1 1 1000 { 20 string cvs cvn pop } for
% Created 1000 names in VM
| String Must Be Readable - String must not have no-access attribute. |
| Names vs. Strings as Keys - Names are more efficient as dictionary keys than strings. |
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
String has no-access attribute |
[ |
String exceeds maximum name length |
[ |
No operand on stack |
[ |
Operand is not a string |
1.9. Implementation Notes
-
Names are stored in a global name table
-
Identical strings convert to the same name object
-
Name table consumes VM (cannot be freed)
-
Executable attribute preserved from string