CoreFW  Check-in [3247812f3b]

Overview
Comment:Fix cfw_strndup() and add cfw_strnlen().
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 3247812f3b23873561a879ec5838a4efbc0c8c4a099b30af5579ad6b01903038
User & Date: js on 2012-09-30 00:59:17
Other Links: manifest | tags
Context
2012-09-30
01:01
Add cfw_stream_read_line(). check-in: 2cee5ea2d5 user: js tags: trunk
00:59
Fix cfw_strndup() and add cfw_strnlen(). check-in: 3247812f3b user: js tags: trunk
00:25
Fix a memory leak in map. check-in: d416cefb3b user: js tags: trunk
Changes

Modified src/string.c from [305d89e8e8] to [6218da6faa].

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
#include "hash.h"

struct CFWString {
	CFWObject obj;
	char *data;
	size_t len;
};












char*
cfw_strdup(const char *s)
{
	char *copy;
	size_t len;

	len = strlen(s);

	if ((copy = malloc(len + 1)) == NULL) {
		errno = ENOMEM;
		return NULL;
	}

	memcpy(copy, s, len);
	copy[len] = 0;

	return copy;
}

char*
cfw_strndup(const char *s, size_t max)
{
	char *copy;
	size_t len;

	len = strlen(s);

	if (len > max)
		len = max;

	if ((copy = malloc(len + 1)) == NULL) {
		errno = ENOMEM;
		return NULL;
	}

	memcpy(copy, s, len);
	copy[len] = 0;

	return copy;
}








>
>
>
>
>
>
>
>
>
>
>









|
<

<













<
<
<
|

|
<

<







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
#include "hash.h"

struct CFWString {
	CFWObject obj;
	char *data;
	size_t len;
};

size_t
cfw_strnlen(const char *s, size_t max)
{
	size_t i = 0;

	for (i = 0; i < max && *s != '\0'; s++)
		i++;

	return i;
}

char*
cfw_strdup(const char *s)
{
	char *copy;
	size_t len;

	len = strlen(s);

	if ((copy = malloc(len + 1)) == NULL)

		return NULL;


	memcpy(copy, s, len);
	copy[len] = 0;

	return copy;
}

char*
cfw_strndup(const char *s, size_t max)
{
	char *copy;
	size_t len;




	len = cfw_strnlen(s, max);

	if ((copy = malloc(len + 1)) == NULL)

		return NULL;


	memcpy(copy, s, len);
	copy[len] = 0;

	return copy;
}

Modified src/string.h from [2f607cb31e] to [01937810ff].

28
29
30
31
32
33
34

35
36
37
38
39
40
41
42
43
44
#define __COREFW_STRING_H__

#include "class.h"
#include "range.h"

typedef struct CFWString CFWString;
extern CFWClass *cfw_string;

extern char* cfw_strdup(const char*);
extern char* cfw_strndup(const char*, size_t);
extern const char* cfw_string_c(CFWString*);
extern size_t cfw_string_length(CFWString*);
extern bool cfw_string_set(CFWString*, const char*);
extern void cfw_string_set_nocopy(CFWString*, char*, size_t);
extern bool cfw_string_append(CFWString*, CFWString*);
extern size_t cfw_string_find(CFWString*, CFWString*, cfw_range_t);

#endif







>










28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#define __COREFW_STRING_H__

#include "class.h"
#include "range.h"

typedef struct CFWString CFWString;
extern CFWClass *cfw_string;
extern size_t cfw_strnlen(const char*, size_t);
extern char* cfw_strdup(const char*);
extern char* cfw_strndup(const char*, size_t);
extern const char* cfw_string_c(CFWString*);
extern size_t cfw_string_length(CFWString*);
extern bool cfw_string_set(CFWString*, const char*);
extern void cfw_string_set_nocopy(CFWString*, char*, size_t);
extern bool cfw_string_append(CFWString*, CFWString*);
extern size_t cfw_string_find(CFWString*, CFWString*, cfw_range_t);

#endif