1.23 class char_array

Class char_array is the superclass of both class name (for unique textual constants) and class string for editable text. Class char_array defines common behaviour and avoids type-conversions if the receiving method is just interested in the text represented. It is not common for the user to create instances of the class directly.

See also class text_buffer for handling large amounts of text, and class regex for searching in text.

See also
class regex

1.23.1 Instance variables

char_array-text: alien:wint_t *
C char * pointer to a’\0’terminated array of characters.

1.23.2 Send methods

char_array ->equal: text=char_array, ignore_case=[bool]
Succeeds if both char_arrays represent the same text. If ignore_case is @on the comparison is executed case-insensitive.

See also char_array->prefix and char_array->suffix.

char_array ->initialise: text=char_array
Create from the argument text. Internally this loop is broken by creating char_array objects from host-language textual constants:
?- new(X, char_array(hello)).

Creates a char_array object from a Prolog atom.

char_array ->larger: than=char_array
@see chain->sort
char_array ->prefix: text=char_array, ignore_case=[bool]
Test if receiver starts with the given argument string. See also char_array->suffix, char_array->equal and class regex.
char_array ->smaller: than=char_array
@see chain->sort
char_array ->sub: text=char_array, ignore_case=[bool]
Test if argument is a substring, If the boolean is @off, the test is ignores possible case differences. See also regex->search.
char_array ->suffix: text=char_array, ignore_case=[bool]
Test if receiver ends with the given string. See also char_array->prefix, char_array<-ensure_suffix and char_array<-delete_suffix.

1.23.3 Get methods

char_array <-append: char_array ... -> char_array
New char array representing the concatenation of the receiver and the argument.
char_array <-base64_encode: -> char_array
char_array <-base64_decode: -> char_array
The methods char_array<-base64_decode and char_array<-base64_encode realise base64 encoding and decoding conform rfc-2045. This encoding technique is commonly used for transferring 8-bit data over 7-bit or otherwise restricted channals.

Encoded data contains the characters 0-9, a-z, A-Z, + and / and end in 0, 1 or 2 _=_ characters. The length of an encode string is always a multiple of 4. char_array<-base64_decode fails silently of the input could not be translated due to a format error.

Base64 encoding is used in transferring E-mail, in the HTTP protocol and many others. See also date<-rfc_string and the class httpd from the library(http/httpd).

char_array <-capitalise: -> char_array
Capitalised version of name. The first character is mapped to its uppercase equivalent. All other letters are mapped to their lowercase equivalent. The letter after a word_separator character (default ('_’)) is mapped to uppercase, while the separator is deleted. Examples:
name			'Name'
label_name		'LabelName'
'label name'	'Label name'

See also char_array<-label_name.

char_array <-character: int -> char
Returns the ASCII value of the nth (0-based) character of the string. Fails silently if the index is out of range.
char_array <-compare: text=char_array, ignore_case=[bool] -> {smaller,equal,larger}
Alphabetically compares the receiver with the argument and returns:
smaller
if the receiver is alphabetically before the argument
equal
If both represented texts are identical
larger
if the receiver is alphabetically after the argument

Intended to be used together with chain->sort and vector->sort.

See also
chain->sort
char_array <-convert: any -> char_array
Converts the following:
subclasses of char_arrayThe text
int, numberDecimal representation of the value
real_%f_ representation of the value
char_array <-copy: -> char_array
Returns an instance of the same class holding the same text. Equivalent to:
get(CA?class, instance, CA).

A subclass for which this behaviour does not produce the desired result should redefine this method. See for example string<-copy.

char_array <-delete_prefix: prefix=char_array -> char_array
If the receiver starts with the argument (see also char_array->prefix), return a new char_array object holding the remaining text. See also char_array<-delete_suffix and char_array->prefix.
char_array <-delete_suffix: suffix=char_array -> char_array
If the receiving char_array ends with suffix, a copy is returned with this suffix deleted. Otherwise this call fails. See also char_array->suffix, char_array<-delete_prefix, char_array<-ensure_suffix and string->ensure_suffix. Example:
?- get('hello.pl', delete_suffix, '.pl', V).

V = hello
char_array <-downcase: -> char_array
Map all uppercase letters to lowercase
char_array <-ensure_suffix: suffix=char_array -> char_array
If the receiving char_array object already has suffix, just return it, otherwise return a copy with the suffix char_array<-append’ed. Examples:
?- get(hello, ensure_suffix, '.pl', X).
?- get('hello.pl', ensure_suffix, '.pl', X).

both yield hello.pl.

char_array <-rindex: of=char, from=[int] -> int
char_array <-index: of=char, from=[int] -> int
Return the index of the given character. When from is given, the search (forwards for char_array<-index, backwards for char_array<-rindex) starts at the given index. Fails silently if the character cannot be located.
char_array <-label_name: -> char_array
This method is used by menu_item<-default_label to compute a nice looking label from a nice looking identifier in the host language. It performs the following transformations:

  1. Any word-separator character (default’_’, for LISP often’-’(see pce->syntax) is transformed into a space.
  2. The first character is capitalised. This method is called as an explicit method on class name by all built-in dialog-related classes to translate identifier names into labels. Below is a skeleton for dealing with labels in international applications.
    :- pce_extend_class(name).
    
    label_name(Name, Label:name) :<-
            (   translate(Name, Label)
            ->  true
            ;   get_class(Name, char_array,
                    label_name, Label)
            ).
    
    :- pce_end_class.
    
    See also
    menu_item<-default_label
char_array <-modify: char_array -> char_array
Called internally by the various get methods that return a modified copy of the char_array (char_array<-append, etc.). The version defined for class char_array is defined as:
modify(CA, To:char_array, Value:char_array) :<-
        get(CA, class, Class),
        get(Class, instance, To, Value).

Redefinition may be necessary for subclasses of class char_array. Examples are the PCE/Lisp predefined class lisp_symbol.

char_array <-read_as_file: from=int, size=int -> char_array
Allow read data from object using pce_open/3. This allows for opening atoms and XPCE string objects as input-stream:
open('hello(world).\n', read, Stream),
read(Stream, X),
close(Stream)

--> X = hello(World)

char_array <-scan: format=char_array -> vector
Parse text according to format. Converted values are returned in the result vector object. This method provides an interface to the C-library function sscanf(3). For details consult your C-library documentation. On most Unix systems this is done using the command man sscanf.

Below is a description of the format string as taken from the SunOs manual.

The control string usually contains conversion specifications, which are used to direct interpretation of input sequences. The control string may contain:

char_array <-split: separator=[char_array] -> chain
Implementation of the popular split-string operatoon. If separator is given, it is used to separate parts of the text. If omitted, a non-empty sequence of layout characters is used. Examples:
?- get_object('foo+bar', split, +, X).
X = chain(foo, bar).

?- get_object('foo bar', split, X).
X = chain(foo, bar).

Please note that -like the other get-methods on class char_array- the returned fragments are of the same class as the receiver. In other words, breaking a name object yields a chain of names and breaking a string object yields a chain of string objects.

See also regex->for_all, which may be used to realise more complicated breaking of text.

char_array <-strip: [{canonicalise,leading,trailing,both}] -> char_array
Canonicalise leading, trailing and internal blanks. Depending on the argument, the behaviour is defined as:

char_array <-sub: start=int, end=[int] -> char_array
Return a copy containing the characters [start ... end). If end is omitted, the end of the char_array is used. Both start end end are 0-based indices.
char_array <-value: -> name
Return the text of the char_array as a name object. On most host-languages this name will be mapped on the language's identifier type (atom for Prolog, keyword for Lisp):
?- get(string("hello"), value, V).

   V = hello