aboutsummaryrefslogtreecommitdiff
path: root/src/util/str.c
blob: 3f9f39c31e00e0a737070720161790f7e5970d10 (plain)
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
/*
 * ecgen, tool for generating Elliptic curve domain parameters
 * Copyright (C) 2017 J08nY
 */
#include "str.h"
#include <ctype.h>
#include <string.h>
#include "util/memory.h"

const char *str_is_hex(const char *hex_str) {
	const char *str_start = hex_str;
	if (strlen(hex_str) > 2) {
		if (hex_str[0] == '0' && (hex_str[1] == 'x' || hex_str[1] == 'X')) {
			str_start = hex_str + 2;
		}
	}
	const char *s = str_start;
	while (*s != 0) {
		char c = *s++;
		if (!isxdigit(c)) return NULL;
	}
	return str_start;
}

char *str_join(char *strings[], size_t len) {
	size_t total = 0;
	for (size_t i = 0; i < len; ++i) {
		if (strings[i]) total += strlen(strings[i]);
	}
	char *result = try_calloc(total);
	for (size_t i = 0; i < len; ++i) {
		if (strings[i]) {
			strcat(result, strings[i]);
		}
	}
	return result;
}