summaryrefslogtreecommitdiff
path: root/src/include/pa_dictionary.h
blob: a8d8c1d33184f0e55926a8b0bd8210f87b645988 (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
/** @file
	Parser: dictionary class decl.

	Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com)
	Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
*/

#ifndef PA_DICTIONARY_H
#define PA_DICTIONARY_H

#define IDENT_PA_DICTIONARY_H "$Id: pa_dictionary.h,v 1.25 2024/11/04 03:53:25 moko Exp $"

#include "pa_table.h"

/// simple dictionary, speding up lookups on contained two columned table
class Dictionary: public PA_Object {
public:

	struct Subst {
		const char *from; size_t from_length;
		const String* to;

		Subst(int): from(0) {}
		Subst(const char* afrom, const String* ato): from(afrom), to(ato) {
			from_length=strlen(afrom);
		}
		operator bool() { return from!=0; }
	};

	/// construct wrapper, grabbing first letters of first column into @b first
	Dictionary(Table& atable);

	/// construct simple dictionary within a single pair only
	Dictionary(const String& from, const String& to);

	/// find first row that contains string in first column which starts @b src
	Subst first_that_begins(const char* str) const;

private:
	Array<Subst> substs;

private:
	void append_subst(const String* from, const String* to, const char* exception=0);

	int starting_line_of[0x100]; int constructor_line;

public:
	size_t count() const { return substs.count(); }

	Subst get(size_t index) const { return substs.get(index); }

};

#endif