-
Notifications
You must be signed in to change notification settings - Fork 1
/
CListInString.h
90 lines (76 loc) · 2.21 KB
/
CListInString.h
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
/////////////////
//
// Práctica de Compilación I (Curso 2010-2011)
//
// FICHERO: CListInString.h
// OBJETIVO: Definir un sistema de representar listas de elementos en un string.
// LICENCIA: Mira el fichero LICENSE en el directorio raíz.
// AUTORES: El equipo del JAG.
//
#ifndef CLISTINSTRING_H
#define CLISTINSTRING_H
#include "CString.h"
#include <vector>
class CListInString
{
public:
static CString EmptyList ()
{
return "()";
}
static bool IsEmpty ( const CString& ls )
{
return ( ls == "()" );
}
static CString InitList ( const CString& strFirstElement )
{
return CString("( ") || strFirstElement || " )";
}
static CString Join ( const CString& left, const CString& right )
{
if ( IsEmpty ( left ) )
return right;
else if ( IsEmpty ( right ) )
return left;
else
{
std::vector<CString> rightElements;
GetListElements ( right, rightElements );
CString ret ( left );
ret.Resize ( ret.Length() - 2 );
for ( std::vector<CString>::iterator it = rightElements.begin ();
it != rightElements.end();
++it )
{
ret.Append ( ", " );
ret.Append ( *it );
}
ret.Append ( " )" );
return ret;
}
}
static std::vector<CString>& GetListElements ( const CString& list, std::vector<CString>& to )
{
if ( list.Length() > 2 && list[0] == '(' && list[list.Length()-1] == ')' )
{
CString copy ( (*list) + 1 );
copy.Resize ( copy.Length() - 1 );
copy.CollapseWhiteSpaces ();
copy.Split ( ',', to );
}
return to;
}
static unsigned int CountListElements ( const CString& list )
{
std::vector<CString> elements;
GetListElements ( list, elements );
return elements.size ();
}
static CString GetItem ( const CString& list, int idx )
{
std::vector<CString> elements;
GetListElements ( list, elements );
return elements [ idx ];
}
};
#endif /* CLISTINSTRING_H */