summaryrefslogtreecommitdiff
path: root/substr.cc
blob: e044f59c83527d3d6b3631fa007f2a178a73e1c8 (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
/* $Id: substr.cc 860 2008-04-09 22:57:45Z nuno-lopes $ */
#include <string.h>
#include <stdlib.h>
#include "substr.h"
#include "globals.h"

#ifndef HAVE_STRNDUP

char *strndup(const char *str, size_t len)
{
	char * ret = (char*)malloc(len + 1);
	
	memcpy(ret, str, len);
	ret[len] = '\0';
	return ret;
}

#endif

namespace re2c
{

void SubStr::out(std::ostream& o) const
{
	o.write(str, len);
}

bool operator==(const SubStr &s1, const SubStr &s2)
{
	return (bool) (s1.len == s2.len && memcmp(s1.str, s2.str, s1.len) == 0);
}

Str::Str(const SubStr& s)
	: SubStr(strndup(s.str, s.len), s.len)
{
	;
}

Str::Str(Str& s)
	: SubStr(s.str, s.len)
{
	s.str = NULL;
	s.len = 0;
}

Str::Str(const char *s)
	: SubStr(strdup(s), strlen(s))
{
	;
}

Str::Str()
	: SubStr((char*) NULL, 0)
{
	;
}


Str::~Str()
{
	if (str) {
		free((void*)str);
	}
	str = NULL;
	len = 0;
}

} // end namespace re2c