CET1@phoenix.cambridge.ac.UK (02/17/89)
Sometimes, when converting a name to a string with "cvs", one needs to know how long the result might be (to avoid a "rangecheck"). This happens in particular when handling the `object being executed' after an error ("command" in "$error"). (I am assuming that if one prepares a string big enough to receive the longest possible name, then one is not going to have any trouble with mere integers, reals, and booleans!) The red book tells me in Appendix B that the longest allowed name on a LaserWriter is 128 bytes, but this is presented merely as being a feature of a particular implementation: it might be different on other PostScript printers, or even in later versions of the PostScript interpreter for the LaserWriter. The way that Adobe's ehandler.ps deals with this problem is to use the useful scratch string stored under the name `=string' in systemdict. This string (also used by the = procedure, hence its name) is 128 bytes long on the LaserWriter, and this would presumably be changed if the limit on the length of a name were changed. However, =string is not documented in the red book (or elsewhere, as far as I know), so using it may be just as chancy as assuming that the limit of 128 will stay the same. Is there any way of determining the upper limit on the length of a name relying only on documented features of PostScript? If not, what method entails the least risk? Chris Thompson JANET: cet1@uk.ac.cam.phx ARPA: cet1%phx.cam.ac.uk@nss.cs.ucl.ac.uk
greid@adobe.com (Glenn Reid) (02/23/89)
In article <9FE19B2A625D3620@UK.AC.CAM.PHX> CET1@phoenix.cambridge.ac.UK writes: Sometimes, when converting a name to a string with "cvs", one needs to know how long the result might be (to avoid a "rangecheck"). This happens in particular when handling the `object being executed' after an error ("command" in "$error"). (I am assuming that if one prepares a string big enough to receive the longest possible name, then one is not going to have any trouble with mere integers, reals, and booleans!) The red book tells me in Appendix B that the longest allowed name on a LaserWriter is 128 bytes, but this is presented merely as being a feature of a particular implementation: it might be different on other PostScript printers, or even in later versions of the PostScript interpreter for the LaserWriter. The way that Adobe's ehandler.ps deals with this problem is to use the useful scratch string stored under the name `=string' in systemdict. This string (also used by the = procedure, hence its name) is 128 bytes long on the LaserWriter, and this would presumably be changed if the limit on the length of a name were changed. However, =string is not documented in the red book (or elsewhere, as far as I know), so using it may be just as chancy as assuming that the limit of 128 will stay the same. Is there any way of determining the upper limit on the length of a name relying only on documented features of PostScript? If not, what method entails the least risk? This is a very good question. Particularly in an error handler, it is good to be as safe as possible about this kind of thing. You are correct in that the 128 byte limit mentioned is an implementation limit. I believe that it is consistent across all existing implementations. However, to be on the safe side, you might consider this technique: % before: /buff 128 string def buff cvs % after: /buff 128 string def dup type /nametype eq { dup length 128 gt { dup length string }{ buff } ifelse }{ buff } ifelse cvs You will allocate a new temporary string if the object you are trying to "cvs" is a name and is larger than 128 bytes, but the likelihood of the code ever getting executed is practically nil. One thing you have to be careful about is if the error happened to be a VMerror, you get into trouble trying to allocate more of it :-) As an aside, if the type of the does not lend itself to being converted to a string, you will get back a string (--nostringval--), which is a little less than helpful (for example, arrays and dictionaries do not have string values). Note: I haven't actually tested this code, but it looks OK. Good luck. Glenn Reid Adobe Systems Developer Tools & Strategies
CET1@phoenix.cambridge.ac.UK (Chris Thompson) (03/01/89)
Thanks to Glenn Reid for his reply to my query, because it has told me something I didn't know about PostScript: you can apply the "length" operator to a name and get the length of the string necessary to hold the results of cvs'ing it. One couldn't have guessed this from the red book, I think. The "length" operator is documented as working on arrays, dictionaries, and strings. It doesn't explicitly say that those are the only things it works on, but one is probably led to assume that, as "typecheck" is one of the errors documented as possibly occurring. In actual fact, experimentation with a LaserWriter (PostScript version 38.0) suggests that "length" can be applied to absolutely anything, although the results aren't always as interesting as they are for a name. All types except arrays, dictionaries, names and strings seem to give the result 1. Chris Thompson JANET: cet1@uk.ac.cam.phx ARPA: cet1%phx.cam.ac.uk@nss.cs.ucl.ac.uk