Charm String Library (string) 

In Charm, native static strings are of type ref array char (reference to an array of characters) with string literals enclosed within double quotes e.g. "This is a string". Native strings are terminated by a char with value 0 and indexed from 0The dynamic string library provides procedures for constructing and working with dynamic (variable content) strings:


Free string

export proc free (ref array char zs)

  • passed zs - string
  1. The space occupied by the string is released back to the heap.
  2. Be careful to only call this method when appropriate. The results of an attempt to access a freed string or a string was not originally allocated on the heap (for instance by one of the other string methods) are undefined.

Get string

export dynamic proc get () ref array char

  • returns wrapped string in native format

Get length

export dynamic proc len () int

  • returns string length
Get string length

   export proc length (ref array char zs) int

  • passed zs - string
  • returns string length
Match strings

export proc match (ref array char zs) boolean

  • passed zs - string to match
  • returns true if the string passed matches this string (same length and content) or false otherwise
Compare strings

export proc compare (ref array char zs1, ref array char zs2) boolean

  • passed zs1 - first string
  •            zs2 - second string
  • returns true if the strings are equal (same length and content) or false otherwise
Find string

export proc find (ref array char zs1, ref array char zs2) int

  • passed zs1 - first string
  •            zs2 - second string
  • returns start index of first occurrence of second string in first string (from 1) or 0 if no match found
Find last

export proc find_last (ref array char zs1, ref array char zs2) int

  • passed zs1 - first string
  •            zs2 - second string
  • returns start index of last occurrence of second string in first string (from 1) or 0 if no match found
Extract sub-string

export proc extract (ref array char zs, int start, int length) ref array char

  • passed zs - string
  •            start - start index of substring (from 1)
  •            length - number of characters to extract
  • returns extracted sub-string
  1. the returned string should be freed if no longer needed to release the memory it occupies.
  2. an error is raised if the requested sub-string start or length are invalid.
Get character

export dynamic proc get_char (int index) char

  • passed index - position of character to get (from 0)
  • returns character from string
  1. an error is raised if the requested index is invalid.

Append string

export dynamic proc str (ref array char zs) ref String

  • passed zs - string to append
  • returns chaining string reference

Create string

export dynamic proc str (ref array char zs) ref String

  • passed zs - initial string content
  • returns chaining string reference

Write integer to buffer

export proc write_int_to_buffer (int value, ref array char buffer, int length) int

  • passed value - value to write to buffer
  •            buffer - buffer
  •            length - buffer length
  • returns number of characters written to buffer

Write real to buffer

export proc write_real_to_buffer (real value, ref array char buffer, int length) int

  • passed value - value to write to buffer
  •            buffer - buffer
  •            length - buffer length
  • returns number of characters written to buffer

Append character

export dynamic proc ch (char ch) ref String

  • passed ch - character to append
  • returns chaining string reference

Append integer

export dynamic proc num (int v) ref String

  • passed number - integer to append
  • returns chaining string reference
Append floating point number

export dynamic proc float (real v) ref String

  • passed v - floating point number to append
  • returns chaining string reference

Append long

export dynamic proc long (int v) ref String

  • passed number - hexadecimal integer to append
  • returns chaining string reference
Clone string

export proc clone (ref array char s) ref array char

  • passed s - string
  • returns copy of string
  1. The returned string is allocated from the heap and can be freed using free string when no longer required.
Check if character is upper case

export proc is_upper (char ch) boolean

  • passed ch - character
  • returns true if character is upper case
Convert string to lower case

export proc to_lower (ref array char zs) ref array char

  • passed zs -  to convert
  • returns new lower case string
  1. The returned string is allocated from the heap and can be freed using free string when no longer required.
Convert string to int

export dynamic proc to_int (ref int zi) boolean

  • passed zi -  int reference to receive converted value
  • returns true if string is a valid integer
Convert string to real

export dynamic proc to_int (ref real zr) boolean

  • passed zr -  real reference to receive converted value
  • returns true if string is a valid real number
Make a Free Website with Yola.