1 /* Copyright (C) 2018
2  *
3  * Permission is hereby granted, free of charge,
4  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
5  * to deal in the Software without restriction, including without limitation the rights to
6  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
7  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8  *
9  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
12  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
14  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
15  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17  */
18 
19 #include "retro_strings.h"
20 
21 #include <ctype.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 // Note: This function returns a pointer to a substring_left of the original string.
27 // If the given string was allocated dynamically, the caller must not overwrite
28 // that pointer with the returned value, since the original pointer must be
29 // deallocated using the same allocator with which it was allocated.  The return
30 // value must NOT be deallocated using free() etc.
trimwhitespace(char * str)31 char* trimwhitespace(char *str)
32 {
33   char *end;
34 
35   // Trim leading space
36   while(isspace((unsigned char)*str)) str++;
37 
38   if(*str == 0)  // All spaces?
39     return str;
40 
41   // Trim trailing space
42   end = str + strlen(str) - 1;
43   while(end > str && isspace((unsigned char)*end)) end--;
44 
45   // Write new null terminator character
46   end[1] = '\0';
47 
48   return str;
49 }
50 
51 // Returns a substring of 'str' that contains the 'len' leftmost characters of 'str'.
strleft(const char * str,int len)52 char* strleft(const char* str, int len)
53 {
54 	char* result = calloc(len + 1, sizeof(char));
55 	strncpy(result, str, len);
56 	return result;
57 }
58 
59 // Returns a substring of 'str' that contains the 'len' rightmost characters of 'str'.
strright(const char * str,int len)60 char* strright(const char* str, int len)
61 {
62 	int pos = strlen(str) - len;
63 	char* result = calloc(len + 1, sizeof(char));
64 	strncpy(result, str + pos, len);
65 	return result;
66 }
67 
68 // Returns true if 'str' starts with 'start'
strstartswith(const char * str,const char * start)69 bool strstartswith(const char* str, const char* start)
70 {
71 	if (strlen(str) >= strlen(start))
72 		if(!strncasecmp(str, start, strlen(start)))
73 			return true;
74 
75 	return false;
76 }
77 
78 // Returns true if 'str' ends with 'end'
strendswith(const char * str,const char * end)79 bool strendswith(const char* str, const char* end)
80 {
81 	if (strlen(str) >= strlen(end))
82 		if(!strcasecmp((char*)&str[strlen(str)-strlen(end)], end))
83 			return true;
84 
85 	return false;
86 }
87