Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cstr.c « base « src - github.com/windirstat/premake-4.x-stable.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ea3bd306a5dcc01b1907d29a8f9dd3cbd4c0b25f (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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
 * \file   cstr.c
 * \brief  C string handling.
 * \author Copyright (c) 2002-2008 Jason Perkins and the Premake project
 */

#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "premake.h"
#include "base/buffers.h"
#include "base/cstr.h"


/**
 * Determines if the sequence appears anywhere in the target string.
 * \param   str      The string to test.
 * \param   expected The sequence to search for.
 * \returns True if the sequence is contained in the string.
 */
int cstr_contains(const char* str, const char* expected)
{
	return (strstr(str, expected) != NULL);
}


/**
 * Determines if the string ends with a particular sequence.
 * \param str        The string to test.
 * \param expected   The sequence for which to look.
 * \returns True if the string ends with the sequence, false otherwise.
 */
int cstr_ends_with(const char* str, const char* expected)
{
	if (str != NULL && expected != NULL)
	{
		int str_len = strlen(str);
		int exp_len = strlen(expected);
		if (str_len >= exp_len)
		{
			const char* start = str + str_len - exp_len;
			return (strcmp(start, expected) == 0);
		}
	}
	return 0;
}


/**
 * Compares two C strings for equality.
 * \param   str        The string to compare.
 * \param   expected   The value to compare against.
 * \returns True if the strings match, zero otherwise.
 */
int cstr_eq(const char* str, const char* expected)
{
	if (str != NULL && expected != NULL)
	{
		return (strcmp(str, expected) == 0);
	}
	return 0;
}


/**
 * Performs a case-insensitive comparasion on two C strings for equality.
 * \param   str      The string to compare.
 * \param   expected The value to compare against.
 * \returns True if the strings match, zero otherwise.
 */
int cstr_eqi(const char* str, const char* expected)
{
	if (str != NULL && expected != NULL)
	{
		while (*str && *expected)
		{
			if (tolower(*str) != tolower(*expected))
			{
				return 0;
			}

			str++;
			expected++;
		}

		return (*str == *expected);
	}
	return 0;
}


/**
 * Builds a string using printf-style formatting codes.
 * \param   format   The format string, which may contain printf-style formatting codes.
 * \returns The formatted string.
 */
char* cstr_format(const char* format, ...)
{
	va_list args;
	char* buffer = buffers_next();

	va_start(args, format);
	vsprintf(buffer, format, args);
	va_end(args);

	return buffer;
}


/**
 * Determines if the given C string starts with a particular sequence.
 * \param   str        The string to test.
 * \param   expected   The sequence for which to look.
 * \returns True if the string starts with the sequence, false otherwise.
 */
int cstr_starts_with(const char* str, const char* expected)
{
	if (str != NULL && expected != NULL)
	{
		return (strncmp(str, expected, strlen(expected)) == 0);
	}
	return 0;
}