Project

General

Profile

String2Array » History » Version 2

iri, 12/17/2011 10:19 PM

1 1 iri
h1. String2Array
2
3 2 iri
<pre>
4 1 iri
/*
5
	Copyright (C) 2011, Stephane Bisaro, aka iri <iri@irizone.net>
6
7
	License : you can do what you want with this source code
8
	This code is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
9
	KIND, either express or implied.
10
*/
11
12
/*
13
	Transform a string to an array
14
	
15
	setArray : fun [S] I
16
		initialize the array from a string. Return always 0.
17
		
18
	changeArray : fun [I S] I
19
		replace the value of the index. Return always 0.
20
		
21
	addArray : fun [S] I
22
		add a char at the end if there is a still space.
23
		Returns 0 if ok, 1 if there is not place enough, 2 if the length
24
		of input is too large (must be one char)
25
		
26
	removeArray : fun [I] I
27
		remove the content of the given index. The content of the
28
		superior index is decreased. Return 0 if ok, 1 if the index
29
		is out of range.
30
		
31
	getArrayValue : fun [I] S
32
		return the char from the given index
33
		
34
	getArray : fun [] S
35
		retrieve a string from the array
36
		
37
	Note : you can define the array with a max size instead the length of 
38
	the input. In this case, don't forget to change tests in functions ...
39
*/
40
41
var input = "Scol is a bbeautiful language ";;
42
typeof array = tab S;;
43
44
/* Private functions */
45
fun getArray2 (counter)=
46
	if counter >= sizetab array then
47
		nil
48
	else
49
		strcat array.counter getArray2 counter+1;;
50
	
51
fun addArray2 (array, char, previous, counter)=
52
	if counter < 0 then
53
		1
54
	else
55
		if array.counter == nil then
56
			addArray2 array char counter counter-1
57
		else if previous != nil then
58
		(
59
			set array.previous = char;
60
			0
61
		)
62
		else
63
			addArray2 array char previous counter-1;;
64
65
fun removeArray2 (array, index, counter)=
66
	if counter >= ((sizetab array) - 1) then
67
	(
68
		set array.counter = nil;
69
		0
70
	)
71
	else if counter >= index then
72
	(
73
		set array.counter = array.(counter+1);
74
		removeArray2 array index counter+1
75
	)
76
	else
77
		removeArray2 array index counter+1;;
78
		
79
fun setArray2 (string, counter)=
80
	if (!strcmp string "") then
81
		0
82
	else
83
		(
84
		set array.counter = substr string 0 1;
85
		setArray2 substr string 1 strlen string counter+1
86
		);;
87
	
88
/* Public functions */
89
fun getArray ()=
90
	getArray2 0;;
91
	
92
fun getArrayValue (index)=
93
	array.index;;
94
95
fun removeArray (index)=
96
	if index > sizetab array then
97
		1
98
	else
99
		removeArray2 array index 0;;
100
		
101
fun addArray (char)=
102
	if 1 == strlen char then
103
		let (sizetab array)-1 -> n in
104
		addArray2 array char nil n
105
	else
106
		2;;
107
108
fun changeArray (index, char)=
109
	set array.index = char;
110
	0;;
111
112
fun setArray (string)=
113
	set array = mktab strlen string "";
114
	setArray2 string 0;;
115
116
/* Main function */	
117
fun main ()=
118
	_showconsole;
119
	
120
	_fooS input;
121
	_fooId setArray input;
122
	_fooS getArrayValue 12;
123
	_fooId changeArray 2 "O";
124
	_fooId removeArray 10;
125
	_fooId addArray "!";
126
	_fooS getArray;
127
	0;;
128
</pre>
129
130
Author : iri
131
Date   : december 2011
132
133
*Return to [[Examples]]*