File: strings.adoc

package info (click to toggle)
nickle 2.107
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 3,756 kB
  • sloc: ansic: 27,954; yacc: 1,874; lex: 954; sh: 204; makefile: 13; lisp: 1
file content (117 lines) | stat: -rw-r--r-- 2,574 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
= Strings

Unlike in C, strings in Nickle are not arrays of or pointers to
individual characters.  Consistent with its pattern of providing
primitive datatypes for types for things that make sense (e.g. `file`
instead of integer file handles), Nickle provides the `string` type.
This has several interesting differences from C-style strings:

* In Nickle, strings are immutable--individual characters may not be changed.
* Strings are, as with everything else, assigned and passed by-value. See the section on Copy semantics for details. 


== Operators

Two useful operators have been overloaded to allow sane manipulation
of strings: `+` and array subscript (`[]`).

=== Subscripting

Although they are not arrays of characters, it is often useful to
access a string a character at a time; the array subscript operator
has been overloaded to allow this.  For example:

----
> string s = "hello, world";
> s[0]
104
> s[1]
101
> s[2]
108
> s[3]
108
> s[4]
111
>
----

Those are the integer representations of each character; they are most
likely in ASCII, but not necessarily--see the section on Unicode in
the I/O section.  The String namespace provides `new` to create a
string from these integer character representations, regardless of
ASCII or Unicode:

`string new(int c)` +
`string new(int[*] cv)`

For instance, 

----
> String::new(s[0])
"h"
----

=== Concatenation

On strings, `+` is the concatenation operator. For example, 

----
> string s = "hello", t = "world"; 
> s = s + ", ";
> t += "!";
> s+t
"hello, world!"
----

== String namespace

In addition, the String namespace provides several useful functions
that facilitate using strings, including the following.

=== Length

`int length ( string s )`

Returns the number of characters in ``s``.
For example, 

----
> String::length ( "hello, world" ) 
12
>
----

=== Index

`int index ( string t, string p )` +
`int rindex ( string t, string p )`

Returns the index of the first occurence of the substring `p` in
``t``, or -1 if `p` is not in ``t``; `rindex` returns the last
occurance instead.  For example,

----
> String::index ( "hello, world", "or" ) 
8
> String::index ( "hello, world", "goodbye" )
-1
> String::rindex ( "hello, world", "o" )
8
----

=== Substr

`string substr ( string s, int i, int l )`

Returns the substring of `s` which begins at index `i` and is `l` characters long.
If `l` is negative, returns the substring of that length which preceeds `i` instead.
For example, 

----
> String::substr ( "hello, world", 8, 2 ) 
"or"
> String::substr ( "hello, world", 8, -4 )
"o, w"
>
----