stacinhtml

Static C in HTML - simple static site generator
git clone git://git.kocotian.pl/stacinhtml.git
Log | Files | Refs | README | LICENSE

str.c (2282B)


      1 /*
      2    stacinhtml - STAtic C IN HTML - simple static site generator
      3    Copyright (C) 2021  Kacper Kocot <kocotian@kocotian.pl>
      4 
      5    This program is free software; you can redistribute it and/or modify
      6    it under the terms of the GNU General Public License as published by
      7    the Free Software Foundation; either version 3 of the License, or
      8    (at your option) any later version.
      9 
     10    This program is distributed in the hope that it will be useful,
     11    but WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13    GNU General Public License for more details.
     14 
     15    You should have received a copy of the GNU General Public License
     16    along with this program; if not, write to the Free Software Foundation,
     17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
     18 
     19 */
     20 
     21 #include "str.h"
     22 
     23 /* String functions */
     24 
     25 String
     26 toString(char *s)
     27 {
     28 	String ret;
     29 	ret.len = strlen(ret.data = s);
     30 	return ret;
     31 }
     32 
     33 int
     34 Strcmp(String a, String b)
     35 {
     36 	if (a.len != b.len) return -1;
     37 	return strncmp(a.data, b.data, a.len);
     38 }
     39 
     40 int
     41 Strcmpc(String s1, char *s2)
     42 {
     43 	if (s1.len != strlen(s2)) return -1;
     44 	return strncmp(s1.data, s2, s1.len);
     45 }
     46 
     47 ssize_t
     48 Strtok(String string, String *out, char c)
     49 {
     50 	char *tmpptr = string.data;
     51 
     52 	if (!string.len) return *out = string, 0;
     53 	if (*(tmpptr) != c)
     54 		while ((unsigned)(tmpptr - string.data) <= string.len) {
     55 			if (*(++tmpptr) == c) break;
     56 		}
     57 	string.len = ++tmpptr - string.data;
     58 	*out = string;
     59 	return (tmpptr - string.data);
     60 }
     61 
     62 String
     63 Striden(String str)
     64 {
     65 	size_t i;
     66 	for (i = 0; ((str.data[i] >= 'a' && str.data[i] <= 'z')
     67 				|| (str.data[i] >= 'A' && str.data[i] <= 'Z')
     68 				|| (str.data[i] && str.data[i] >= '0' && str.data[i] <= '9'))
     69 				&& (i < str.len); ++i);
     70 	str.len = i;
     71 	return str;
     72 }
     73 
     74 String
     75 Strtrim(String str)
     76 {
     77 	size_t i = 0;
     78 	while (isspace(*(str.data)) && i < str.len) {
     79 		++str.data;
     80 		--str.len;
     81 		++i;
     82 	}
     83 	while (isspace(*(str.data + (str.len - 1))))
     84 		--str.len;
     85 	return str;
     86 }
     87 
     88 /* Array functions */
     89 int
     90 _inArray(char *data, size_t len, void *val, size_t vlen)
     91 {
     92 	size_t i, n;
     93 	for (n = i = 0; i < len; ++i, data += vlen)
     94 		if (!memcmp(data, val, vlen)) ++n;
     95 	return (int)n;
     96 }
     97 
     98 void *
     99 _prepareArray(void *data, size_t siz)
    100 {
    101 	return memset(data, 0, siz);
    102 }