-
Notifications
You must be signed in to change notification settings - Fork 13
/
uuString.r
83 lines (79 loc) · 2.44 KB
/
uuString.r
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
# \file uuString.r
# \brief String functions.
# \author Chris Smeele
# \author Ton Smeele
# \copyright Copyright (c) 2015, Utrecht University. All rights reserved.
# \license GPLv3, see LICENSE.
# \brief Chop part of a string based on a split character.
#
# if leftToRight is true, *head will contain *string up to the first *splitChar,
# and *tail will contain the rest of the string.
# Otherwise, *tail will contain the part of the string from the last *splitChar.
# and *head will contain the rest of the string.
#
# *string is not modified.
#
# \param[in] string
# \param[out] head
# \param[out] tail
# \param[in] splitChar the character on which to split the string
# \param[in] leftToRight true if we should chop from the left side
#
uuChop(*string, *head, *tail, *splitChar, *leftToRight) {
if (*string like "**splitChar*") {
if (*leftToRight) {
*tail = triml(*string, *splitChar);
*head = substr(*string, 0, strlen(*string) - strlen(*tail) - 1);
} else {
*head = trimr(*string, *splitChar);
*tail = substr(*string, strlen(*head) + 1, strlen(*string));
}
} else {
# No *splitChar in *string.
*head = if *leftToRight then "" else *string;
*tail = if *leftToRight then *string else "";
}
}
# \brief Split a file name into a base name and a file extension.
#
# \param[in] fileName
# \param[out] baseName
# \param[out] extension
#
uuChopFileExtension(*fileName, *baseName, *extension) {
uuChop(*fileName, *baseName, *extension, ".", false);
}
# \brief Split a path into a base name and a path.
#
# \param[in] fileName
# \param[out] parent
# \param[out] baseName
#
uuChopPath(*path, *parent, *baseName) {
if (*path like regex "^/[^/]*$") {
# *path is "/" or a top level directory.
*baseName = if strlen(*path) > 1 then substr(*path, 1, strlen(*path)) else "/";
*parent = "/";
} else {
uuChop(*path, *parent, *baseName, "/", false);
}
}
# \brief Split a checksum into a checksum type and a value
#
# \param[in] checksum
# \param[out] checksumType e.g. "md5" or "sha2"
# \param[out] checksumValue
#
uuChopChecksum(*checksum, *checksumType, *checksumValue) {
# if checksum is not labeled then it is "md5"
*checksumType = "md5";
*checksumValue = *checksum;
*checksumParts = split(*checksum, ":");
if (size(*checksumParts) > 1 ) {
*checksumType = hd(*checksumParts);
*checksumValue = "";
foreach (*value in tl(*checksumParts)) {
*checksumValue = "*checksumValue*value";
}
}
}