klisp

an open source interpreter for the Kernel Programming Language.
git clone http://git.hanabi.in/repos/klisp.git
Log | Files | Refs | README

Strings.html (13124B)


      1 <html lang="en">
      2 <head>
      3 <title>Strings - klisp Reference Manual</title>
      4 <meta http-equiv="Content-Type" content="text/html">
      5 <meta name="description" content="klisp Reference Manual">
      6 <meta name="generator" content="makeinfo 4.13">
      7 <link title="Top" rel="start" href="index.html#Top">
      8 <link rel="prev" href="Numbers.html#Numbers" title="Numbers">
      9 <link rel="next" href="Characters.html#Characters" title="Characters">
     10 <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
     11 <meta http-equiv="Content-Style-Type" content="text/css">
     12 <style type="text/css"><!--
     13   pre.display { font-family:inherit }
     14   pre.format  { font-family:inherit }
     15   pre.smalldisplay { font-family:inherit; font-size:smaller }
     16   pre.smallformat  { font-family:inherit; font-size:smaller }
     17   pre.smallexample { font-size:smaller }
     18   pre.smalllisp    { font-size:smaller }
     19   span.sc    { font-variant:small-caps }
     20   span.roman { font-family:serif; font-weight:normal; } 
     21   span.sansserif { font-family:sans-serif; font-weight:normal; } 
     22 --></style>
     23 <link rel="stylesheet" type="text/css" href="css/style.css">
     24 </head>
     25 <body>
     26 <div class="node">
     27 <a name="Strings"></a>
     28 <p>
     29 Next:&nbsp;<a rel="next" accesskey="n" href="Characters.html#Characters">Characters</a>,
     30 Previous:&nbsp;<a rel="previous" accesskey="p" href="Numbers.html#Numbers">Numbers</a>,
     31 Up:&nbsp;<a rel="up" accesskey="u" href="index.html#Top">Top</a>
     32 <hr>
     33 </div>
     34 
     35 <!-- node-name,  next,  previous,  up -->
     36 <h2 class="chapter">15 Strings</h2>
     37 
     38 <p><a name="index-strings-232"></a>
     39   A string is an object that represent a sequence of characters (for
     40 now, only ASCII is supported in klisp, in the future, full UNICODE
     41 will be supported).  The external representation of strings consists
     42 of a leading &ldquo;"&rdquo;, the characters of the string and a closing &ldquo;"&rdquo;. 
     43 Both double quote and backslash should be escaped to appear withing
     44 strings.  Some other characters also have an escaped form for
     45 convenience.  All of these are written with a leading slash (&ldquo;\&rdquo;). 
     46 In klisp these are: double quote (&ldquo;\"&rdquo;), backslash (&ldquo;\\&rdquo;), null
     47 (&ldquo;\0&rdquo;), alarm (&ldquo;\a&rdquo;), backspace (&ldquo;\b&rdquo;), tab (&ldquo;\t&rdquo;), newline
     48 (&ldquo;\n&rdquo;), return (&ldquo;\r&rdquo;), vertical tab (&ldquo;\v&rdquo;), and formfeed
     49 (&ldquo;\f&rdquo;).  You can also use inline hex escapes to include arbitary
     50 unicode codepoints (only ASCII range supported for now).  The syntax
     51 is &ldquo;\x&lt;hex codepoint&gt;;&rdquo;.  New lines can be escaped to simplify the
     52 accomodation of literal strings in source code, to do this: use &ldquo;\&rdquo;
     53 followed by any ammount of intraline whitespace, a new line and
     54 another ammount of intraline whitespace.  All of that intraline
     55 whitespace and the newline, together with the leading slash is
     56 discarded by the reader and doesn't end up in the string being read.
     57 
     58    <p>A string has a length that is fixed at creation time, and as many
     59 characters, indexed from <code>0</code> to <code>length-1</code>.
     60 
     61    <p>Strings may be mutable or immutable.  If an attempt is made to
     62 mutate an immutable string, an error is signaled.  Two immutable
     63 strings are &ldquo;eq?&rdquo; iff they are &ldquo;equal?&rdquo;.  Two mutable strings are
     64 &ldquo;eq?&rdquo; if they were created by the same constructor call.  Two
     65 mutable strings are &ldquo;equal?&rdquo; iff they are &ldquo;string=?&rdquo;.  For now it
     66 is undefined if a mutable and an immutable strings that are
     67 &ldquo;string=?&rdquo; are &ldquo;equal?&rdquo; or not.  The only exception is the empty
     68 string.  There is only one empty string (all empty strings are &ldquo;eq?&rdquo;
     69 to each other) and it should be considered immutable.  Even if an
     70 attempt is made to return a new empty string (like calling
     71 <code>(string)</code>, the canonical immutable empty string is returned. 
     72 The string type is encapsulated.
     73 
     74    <p>SOURCE NOTE: This section is still missing from the report.  The
     75 features defined here were taken mostly from r7rs scheme.  It is
     76 possible that in the future, klisp only admits immutable strings (like
     77 lua and java), and that operations for contructing strings are moved
     78 to a new type (like Java's StringBuilder/StringBuffer).  But for now,
     79 compatibility with r7rs was preferred/simpler.
     80 
     81 <div class="defun">
     82 &mdash; Applicative: <b>string?</b> (<var>string? . objects</var>)<var><a name="index-string_003f-233"></a></var><br>
     83 <blockquote><p>  The primitive type predicate for type string.  <code>string?</code>
     84 returns true iff all the objects in <code>objects</code> are of type
     85 string. 
     86 </p></blockquote></div>
     87 
     88 <div class="defun">
     89 &mdash; Applicative: <b>immutable-string?</b> (<var>immutable-string? objects</var>)<var><a name="index-immutable_002dstring_003f-234"></a></var><br>
     90 &mdash; Applicative: <b>mutable-string?</b> (<var>mutable-string? objects</var>)<var><a name="index-mutable_002dstring_003f-235"></a></var><br>
     91 <blockquote><p>The primitive type predicates for types immutable string and mutable
     92 string.  These return true iff all the objects in <code>objects</code> are of
     93 type immutable string or mutable string respectively.
     94 
     95         <p>SOURCE NOTE: these aren't provided in the Kernel report, but added for
     96 convenience.  These can be implemented in standard kernel by using guards. 
     97 </p></blockquote></div>
     98 
     99 <div class="defun">
    100 &mdash; Applicative: <b>string=?</b> (<var>string=? . strings</var>)<var><a name="index-string_003d_003f-236"></a></var><br>
    101 &mdash; Applicative: <b>string&lt;?</b> (<var>string&lt;? . strings</var>)<var><a name="index-string_003c_003f-237"></a></var><br>
    102 &mdash; Applicative: <b>string&lt;=?</b> (<var>string&lt;=? . strings</var>)<var><a name="index-string_003c_003d_003f-238"></a></var><br>
    103 &mdash; Applicative: <b>string&gt;?</b> (<var>string&gt;? . strings</var>)<var><a name="index-string_003e_003f-239"></a></var><br>
    104 &mdash; Applicative: <b>string&gt;=?</b> (<var>string&gt;=? . strings</var>)<var><a name="index-string_003e_003d_003f-240"></a></var><br>
    105 <blockquote><p>  These predicates compare any number of strings by their
    106 lexicographic order. 
    107 </p></blockquote></div>
    108 
    109 <div class="defun">
    110 &mdash; Applicative: <b>string-ci=?</b> (<var>string-ci=? . strings</var>)<var><a name="index-string_002dci_003d_003f-241"></a></var><br>
    111 &mdash; Applicative: <b>string-ci&lt;?</b> (<var>string-ci&lt;? . strings</var>)<var><a name="index-string_002dci_003c_003f-242"></a></var><br>
    112 &mdash; Applicative: <b>string-ci&lt;=?</b> (<var>string-ci&lt;=? . strings</var>)<var><a name="index-string_002dci_003c_003d_003f-243"></a></var><br>
    113 &mdash; Applicative: <b>string-ci&gt;?</b> (<var>string-ci&gt;? . strings</var>)<var><a name="index-string_002dci_003e_003f-244"></a></var><br>
    114 &mdash; Applicative: <b>string-ci&gt;=?</b> (<var>string-ci&gt;=? . strings</var>)<var><a name="index-string_002dci_003e_003d_003f-245"></a></var><br>
    115 <blockquote><p>  These predicates convert the strings to lowercase and then compare
    116 them using their lexicographic order. 
    117 </p></blockquote></div>
    118 
    119 <div class="defun">
    120 &mdash; Applicative: <b>make-string</b> (<var>make-string k </var>[<var>char</var>])<var><a name="index-make_002dstring-246"></a></var><br>
    121 <blockquote><p>  Applicative <code>make-string</code> constructs and returns a new mutable
    122 string of length <code>k</code>.  If <code>char</code> is specified, then all
    123 characters in the returned string are <code>char</code>, otherwise the
    124 content of the string is unspecified. 
    125 </p></blockquote></div>
    126 
    127 <div class="defun">
    128 &mdash; Applicative: <b>string</b> (<var>string . chars</var>)<var><a name="index-string-247"></a></var><br>
    129 <blockquote><p>  Applicative <code>string</code> contructs and return a new mutable string
    130 composed of the character arguments. 
    131 </p></blockquote></div>
    132 
    133 <div class="defun">
    134 &mdash; Applicative: <b>string-length</b> (<var>string-length string</var>)<var><a name="index-string_002dlength-248"></a></var><br>
    135 <blockquote><p>  Applicative <code>string-length</code> returns the length of
    136 <code>string</code>. 
    137 </p></blockquote></div>
    138 
    139 <div class="defun">
    140 &mdash; Applicative: <b>string-ref</b> (<var>string-ref string k</var>)<var><a name="index-string_002dref-249"></a></var><br>
    141 <blockquote><p>  Applicative <code>string-ref</code> returns the character of <code>string</code>
    142 at position <code>k</code>.  If <code>k</code> is out of bounds (i.e. less than
    143 <code>0</code> or greater or equal than <code>(string-length string)</code>) an error is
    144 signaled. 
    145 </p></blockquote></div>
    146 
    147 <div class="defun">
    148 &mdash; Applicative: <b>string-set!</b> (<var>string-set! string k char</var>)<var><a name="index-string_002dset_0021-250"></a></var><br>
    149 <blockquote><p>  Applicative <code>string-set!</code> replaces the character with index
    150 <code>k</code> in <code>string</code> with character <code>char</code>.  If <code>k</code> is
    151 out of bounds, or <code>string</code> is immutable, an error is signaled. 
    152 </p></blockquote></div>
    153 
    154 <div class="defun">
    155 &mdash; Applicative: <b>string-fill!</b> (<var>string-fill! string char</var>)<var><a name="index-string_002dfill_0021-251"></a></var><br>
    156 <blockquote><p>  Applicative <code>string-fill!</code> replaces all the characters in
    157 <code>string</code> with character <code>char</code>.  If <code>string</code> is an
    158 immutable string, an error is signaled. 
    159 </p></blockquote></div>
    160 
    161 <div class="defun">
    162 &mdash; Applicative: <b>substring</b> (<var>substring string k1 k2</var>)<var><a name="index-substring-252"></a></var><br>
    163 <blockquote><p>  Both <code>k1</code> &amp; <code>k2-1</code> should be valid indexes in
    164 <code>string</code>.  Also it should be the case that <code>k1 &lt;= k2</code>.
    165 
    166         <p>Applicative <code>substring</code> constructs and returns a new mutable
    167 string with length <code>k2 - k1</code>, with the characters from
    168 <code>string</code>, starting at index <code>k1</code> (inclusive) and ending at
    169 index <code>k2</code> (exclusive). 
    170 </p></blockquote></div>
    171 
    172 <div class="defun">
    173 &mdash; Applicative: <b>string-append</b> (<var>string-append . strings</var>)<var><a name="index-string_002dappend-253"></a></var><br>
    174 <blockquote><p>  Applicative <code>string-append</code> constructs and returns a new
    175 mutable string consisting of the concatenation of all its arguments. 
    176 </p></blockquote></div>
    177 
    178 <div class="defun">
    179 &mdash; Applicative: <b>string-copy</b> (<var>string-copy string</var>)<var><a name="index-string_002dcopy-254"></a></var><br>
    180 <blockquote><p>  Applicative <code>string-copy</code> constructs and returns a new mutable
    181 string with the same length and characters as <code>string</code>. 
    182 </p></blockquote></div>
    183 
    184 <div class="defun">
    185 &mdash; Applicative: <b>string-&gt;immutable-string</b> (<var>string-&gt;immutable-string string</var>)<var><a name="index-string_002d_003eimmutable_002dstring-255"></a></var><br>
    186 <blockquote><p>  Applicative <code>string-&gt;immutable-string</code> constructs and returns a
    187 new immutable string with the same length and characters as
    188 <code>string</code>. 
    189 </p></blockquote></div>
    190 
    191 <div class="defun">
    192 &mdash; Applicative: <b>string-&gt;list</b> (<var>string-&gt;list string</var>)<var><a name="index-string_002d_003elist-256"></a></var><br>
    193 &mdash; Applicative: <b>list-&gt;string</b> (<var>list-&gt;string chars</var>)<var><a name="index-list_002d_003estring-257"></a></var><br>
    194 &mdash; Applicative: <b>string-&gt;vector</b> (<var>string-&gt;vector string</var>)<var><a name="index-string_002d_003evector-258"></a></var><br>
    195 &mdash; Applicative: <b>vector-&gt;string</b> (<var>vector-&gt;string vchars</var>)<var><a name="index-vector_002d_003estring-259"></a></var><br>
    196 &mdash; Applicative: <b>string-&gt;bytevector</b> (<var>string-&gt;bytevector string</var>)<var><a name="index-string_002d_003ebytevector-260"></a></var><br>
    197 &mdash; Applicative: <b>bytevector-&gt;string</b> (<var>bytevector-&gt;string bvchars</var>)<var><a name="index-bytevector_002d_003estring-261"></a></var><br>
    198 <blockquote><p>These applicatives convert between strings and list of characters,
    199 vectors of characters, and bytevectors of characters.  The objects
    200 returned by these applicatives are always mutable. 
    201 </p></blockquote></div>
    202 
    203 <div class="defun">
    204 &mdash; Applicative: <b>string-upcase</b> (<var>string-upcase string</var>)<var><a name="index-string_002dupcase-262"></a></var><br>
    205 &mdash; Applicative: <b>string-downcase</b> (<var>string-downcase string</var>)<var><a name="index-string_002ddowncase-263"></a></var><br>
    206 &mdash; Applicative: <b>string-titlecase</b> (<var>string-titlecase string</var>)<var><a name="index-string_002dtitlecase-264"></a></var><br>
    207 &mdash; Applicative: <b>string-foldcase</b> (<var>string-foldcase string</var>)<var><a name="index-string_002dfoldcase-265"></a></var><br>
    208 <blockquote><p>These applicatives perform the respective case folding on the passed
    209 <code>string</code> and return a new mutable strings as a result.  The
    210 original <code>string</code> is not modified.  For now in klisp only ASCII
    211 is implemented, and so <code>string-foldcase</code> is the same as
    212 <code>string-downcase</code>. 
    213 </p></blockquote></div>
    214 
    215 <!-- *-texinfo-*- -->
    216    </body></html>
    217