summaryrefslogtreecommitdiff
path: root/StringList.cc
blob: 71e5c77f1b2375430f7e00481bc5e896c7fba014 (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
/*
  © Copyright 2008 Randal A. Koene <randalk@netmorph.org>
  
  With design assistance from J. van Pelt & A. van Ooyen, and support
  from the Netherlands Organization for Scientific Research (NWO)
  Program Computational Life Sciences grant CLS2003 (635.100.005) and
  from the EC Marie Curie Research and Training Network (RTN)
  NEURoVERS-it 019247.

  This file is part of NETMORPH.

  NETMORPH 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 3 of the License, or
  (at your option) any later version.

  NETMORPH 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 NETMORPH.  If not, see <http://www.gnu.org/licenses/>.
*/
// StringList.cc
// Randal A. Koene, 20041119

#include "StringList.hh"

StringList::StringList(String cs, const char sep) {
// initialize list from a string with a specified separator
// (Note: In the current implementation this leaves an unused
// list element at the end of the list.
// Beware! If you fix this to leave no unused list element at
// the end of the list you must check all source code where
// this initialization function is used to insure that the
// code is modified so as not to depend on this "feature"!)
	Next=NULL;
	split(0,cs,sep);
}

String & StringList::operator[](unsigned long n) {
	if (!n) return s;
	if (!Next) Next = new StringList();
	return (*Next)[n-1];
}

StringList * StringList::List_Index(long n) {
  if (n<0) return NULL;
  if (!n) return this;
  if (!Next) return NULL;
  return Next->List_Index(n-1);
}

void StringList::append(unsigned long n, String & ins) {
	if (!n) Next = new StringList(ins,Next);
	else
		if (Next) Next->append(n-1,ins);
		else {
			Next = new StringList();
			Next->append(n-1,ins);
		}
}

void StringList::insert(unsigned long n, String & ins) {
	if (!n) { Next = new StringList(s,Next); s = ins; }
	else
		if (Next) Next->insert(n-1,ins);
		else 
			if (n>1) {
				Next = new StringList();
				Next->insert(n-1,ins);
			} else Next = new StringList(ins);
}

long StringList::iselement(String el, unsigned long n) {
	if (el == s) return n;
	else {
		if (Next) return Next->iselement(el,n+1);
		else return -1;
	}
}

long StringList::contains(String substr, unsigned long n) {
	if (s.contains(substr)) return n;
	else {
		if (Next) return Next->contains(substr,n+1);
		else return -1;
	}
}

long StringList::first_contained_in(String & sourcestr, unsigned long n) {
  if (sourcestr.contains(s)) return n;
  else {
    if (Next) return Next->first_contained_in(sourcestr,n+1);
    else return -1;
  }
}

unsigned long StringList::split(unsigned long n, String cs, const char sep) {
// insert list elements from a string with a specified separator
	if (cs.length()<=0) return length();
	String el;
	if (cs.index(sep)>=0) el = cs.before(sep); else el = cs;
	// *** this is not a speed optimized implementation
	insert(n,el);
	split(n+1,cs.after(sep),sep);
	return length();
}

String StringList::concatenate(String sepstr) {
	if (Next) return s+sepstr+Next->concatenate(sepstr);
	else return s;
}