File: String.c

package info (click to toggle)
openclonk 8.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 169,656 kB
  • sloc: cpp: 180,484; ansic: 108,988; xml: 31,371; python: 1,223; php: 767; makefile: 148; sh: 101; javascript: 34
file content (52 lines) | stat: -rw-r--r-- 1,324 bytes parent folder | download | duplicates (5)
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
/**
	String.c
	Functions for string manipulation.	
	
	@author Maikel
*/

// Returns the reduced string with only the characters in the interval [begin, end) are taken. The value begin starts
// at zero and end can be at most the length of the string, which then includes the last character.
global func TakeString(string str, int begin, int end)
{
	// Default values and safety.
	begin = begin ?? 0;
	begin = Max(begin, 0);
	end = end ?? GetLength(str);
	end = Min(end, GetLength(str));
	// Construct the reduced string by looping over all chars.
	var reduced_str = "";
	for (var index = begin; index < end; index++)
		reduced_str = Format("%s%c", reduced_str, GetChar(str, index));
	return reduced_str;
}

// Converts a char into a string.
global func CharToString(int char)
{
	return Format("%c", char);
}

// Returns whether a char is a digit [0-9].
global func CharIsDigit(int char)
{
	return Inside(char, 48, 57);
}

// Returns whether a char is a letter [A-Za-z].
global func CharIsLetter(int char)
{
	return CharIsLowerCase(char) || CharIsUpperCase(char);
}

// Returns whether a char is a lower-case letter [a-z].
global func CharIsLowerCase(int char)
{
	return Inside(char, 97, 122);
}

// Returns whether a char is an upper-case letter [A-Z].
global func CharIsUpperCase(int char)
{
	return Inside(char, 65, 90);
}