All Manuals > LispWorks® User Guide and Reference Manual > 26 Internationalization: characters, strings and encodings

26.5 String Construction

LispWorks constructs strings of a suitable type where sufficient information is available. Failing that, strings are constructed of type according to the value of *default-character-element-type*.

26.5.1 Default string construction

If the value of *default-character-element-type* is base-char then:

(make-string 3) 

returns a simple-base-string and:

(coerce sequence 'simple-string)

attempts to construct a simple-base-string. This will signal an error if any element of sequence is not a base-char.

If the value of *default-character-element-type* is cl:character then:

(make-string 3) 

returns a simple-text-string and:

(coerce sequence 'simple-string)

attempts to construct a simple-text-string. This will signal an error if any element of sequence is not a cl:character.

Other string constructors also take their default from *default-character-element-type*. For instance, with-output-to-string and make-string-output-stream will construct a stream with element type determined by this variable and generate a string of the same element type.

Also, the string reader will always construct a string of type determined by *default-character-element-type*, unless it sees a character of a larger type, in which case a suitable string is constructed. For example:

CL-USER 1 > (set-default-character-element-type 'character)
CHARACTER
 
CL-USER 2 > (type-of "ABC")
SIMPLE-TEXT-STRING

Compatibility note: In LispWorks 6.0 and earlier versions, the string reader would not always obey *default-character-element-type*, due to a bug.

26.5.2 String construction with known type

The parameter *default-character-element-type* merely provides the default behavior. If enough information is supplied, then a string of suitable type is constructed. For instance, the form:

(make-string 3 :initial-element #\Ideographic-Space)

constructs a string of a type that can hold its elements, regardless of the value of *default-character-element-type*.

Likewise, format nil, princ-to-string, prin1-to-string and write-to-string will return a string whose element type can hold all characters that are written.

Functions that have a sequence type specifier as an argument, such as concatenate, use it as described in 26.3.5 String types.

26.5.3 Controlling string construction

The initial value of *default-character-element-type* is base-char, to avoid programs that only require 8-bit strings needlessly creating larger string objects. If your application uses Unicode characters beyond the Latin-1 range (characters of type extended-char) then you should consider which of the following two approaches to use:

Note: Do not attempt to bind or set directly the variable *default-character-element-type*. Instead, call set-default-character-element-type.

26.5.4 String construction on Windows systems

When LispWorks for Windows starts up on a OS with a non-Latin-1 code page, it calls:

(set-default-character-element-type 'cl:character)

so that by default, newly constructed strings can contain the data likely to be returned from the OS or user input.

If you know your string only needs to contain 8-bit data, then you can create it explicitly with element type base-char.

Conversely if you know that a string may need to contain 16-bit data even on a Latin-1 code page system, then you should create it explicitly with element type bmp-char (or cl:character if 32-bit data is needed).


LispWorks® User Guide and Reference Manual - 01 Dec 2021 19:30:24