File: MnemonicStr.cpp

package info (click to toggle)
storm-lang 0.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 51,984 kB
  • sloc: ansic: 261,420; cpp: 140,270; sh: 14,877; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (90 lines) | stat: -rw-r--r-- 1,862 bytes parent folder | download | duplicates (3)
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
#include "stdafx.h"
#include "MnemonicStr.h"

namespace gui {

	MnemonicStr::MnemonicStr(Str *value) : value(value), hasPrefixes(false) {}

	void MnemonicStr::deepCopy(CloneEnv *) {
		// Nothing to do, strings are immutable.
	}

	Str *MnemonicStr::plain() const {
		if (!hasPrefixes)
			return value;

		StrBuf *out = new (value) StrBuf();
		bool lastUnderscore = false;
		for (Str::Iter i = value->begin(); i != value->end(); ++i) {
			bool underscore = i.v() == Char('_');

			if (underscore && lastUnderscore) {
				underscore = false;
			}

			if (!underscore) {
				*out << i.v();
			}

			lastUnderscore = underscore;
		}

		return out->toS();
	}

	Str *MnemonicStr::mnemonic(Char ch) const {
		if (hasPrefixes && ch == Char('_'))
			return value;

		StrBuf *out = new (value) StrBuf();
		if (hasPrefixes) {
			// Need to convert...
			bool lastUnderscore = false;
			for (Str::Iter i = value->begin(); i != value->end(); ++i) {
				bool underscore = i.v() == Char('_');

				if (underscore && lastUnderscore) {
					// No longer need to be escaped.
					underscore = false;
					lastUnderscore = false;
				}

				if (!underscore) {
					if (lastUnderscore) {
						// We want this to be underlined.
						*out << ch << i.v();
					} else if (i.v() == ch) {
						// Need to escape this character...
						*out << ch << ch;
					} else {
						*out << i.v();
					}
				}

				lastUnderscore = underscore;
			}
		} else {
			// Just need to escape 'ch' by duplicating it.
			for (Str::Iter i = value->begin(); i != value->end(); ++i) {
				if (i.v() == ch)
					*out << ch;
				*out << i.v();
			}
		}
		return out->toS();
	}

	void MnemonicStr::toS(StrBuf *to) const {
		if (hasPrefixes)
			*to << S("Mnemonic: ") << value;
		else
			*to << value;
	}

	MnemonicStr mnemonic(Str *value) {
		MnemonicStr x(value);
		x.hasPrefixes = true;
		return x;
	}

}