File: String.cc

package info (click to toggle)
hatman 0.5.0.1-4
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,416 kB
  • ctags: 1,971
  • sloc: cpp: 7,871; sh: 330; yacc: 322; ansic: 260; lex: 210; makefile: 104; awk: 80
file content (78 lines) | stat: -rw-r--r-- 2,096 bytes parent folder | download | duplicates (2)
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
/*C* $Id: str.cc,v 1.3 1997/09/27 00:08:56 james Exp james $
 *
 * Hatman - The Game of Kings
 * Copyright (C) 1997 James Pharaoh & Timothy Fisken
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *C*/

#include "Collection.h"
#include "String.h"
#include <string.h>

template class Collection<String>;

String& String::operator = (String& string)
{
 delete s;
 s = newString(string.s);
 return *this;
}

String& String::operator = (const char* object)
{
 delete s;
 s = newString(object);
 return *this;
}

String& String::operator += (const char* object)
{
 char* ns = new char[length() + strlen(object) + 1];
 strcpy(ns, s); strcat(ns, object);
 delete s; s = ns; return *this;
}

String& String::operator += (const char object)
{
 int n = length();
 char* ns = new char[n + 2];
 strcpy(ns, s); ns[n] = object; ns[n+1] = '\0';
 delete s; s = ns; return *this;
}

String String::operator + (const char* object)
{
 int n = length();
 char* ns = new char[n + strlen(object) + 1];
 strcpy(ns, s); strcpy(ns + n, object);
 String string(ns); delete ns; return string;
}

String String::operator + (const char object)
{
 int n = length();
 char* ns = new char[n + 2];
 strcpy(ns, s); ns[n] = object; ns[n+1] = '\0';
 String string(ns); delete ns; return string;
}

bool String::endsin(const char* end)
{
 if(strlen(s) < strlen(end)) return false;
 if(strcmp(s + strlen(s) - strlen(end), end) != 0) return false;
 return true;
}