1. cvs
Converts any object to its string representation.
1.2. Stack Effects
| Level | Object |
|---|---|
1 |
|
0 |
|
| Level | Object |
|---|---|
0 |
|
1.3. Description
cvs (convert to string) produces a text representation of an arbitrary object and stores it into the supplied string, overwriting an initial portion of its value. It returns a string object designating the substring actually used.
Conversion rules:
* Numbers: Text representation (e.g., 123, 3.14)
* Booleans: true or false
* Strings: Contents copied verbatim
* Names/operators: Text representation of name
* Other types: --nostringval--
For real numbers, the exact format is implementation-dependent (e.g., 0.001 or 1.0E-3).
1.5. Examples
/str 20 string def
123 456 add str cvs
% Result: (579)
/str 10 string def
true str cvs % Result: (true)
false str cvs % Result: (false)
/str 20 string def
/MyName str cvs % Result: (MyName)
/str 20 string def
mark str cvs % Result: (--nostringval--)
1.6. Common Use Cases
1.7. Common Pitfalls
Buffer Must Be Large Enough - If string is too small, [rangecheck] error occurs.
|
123456 3 string cvs % Error: need at least 6 chars
| Real Number Format Varies - Format for reals is implementation-dependent. |
0.001 20 string cvs % Might be (0.001) or (1.0E-3)
| Returns Substring - Result is a substring sharing the buffer, not a new string. |
/buf 20 string def
123 buf cvs /s1 exch def
456 buf cvs /s2 exch def
% s1 now shows (456...) - buffer was reused!
| Use Ample Buffer Size - Allocate at least 20-30 characters for number conversions. |
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
String has no-access or read-only attribute |
[ |
String too small for conversion result |
[ |
Fewer than 2 operands on stack |
[ |
Second operand is not a string |
1.9. Implementation Notes
-
Numbers converted using PostScript syntax rules
-
String result shares the buffer (not a copy)
-
For strings, performs a copy operation
-
Return value points to used portion of buffer only