aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/normalize_prefix.cpp
blob: f85a341a17b74a5c6817e7b44a6614671f1e0277 (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
/*
	normalize prefix
*/
#include <string>
#include <set>
#include <iostream>
#include <memory.h>

typedef unsigned char uint8_t;

std::string normalize(std::string line)
{
	size_t pos = line.find('(');
	/* nasm generates byte codes containing () for xbegin, so remove it. */
	if (pos != std::string::npos) {
		line.erase(pos, 1);
		pos = line.find(')');
		if (pos == std::string::npos) {
			fprintf(stderr, "line error {%s}\n", line.c_str());
			return "";
		}
		line.erase(pos, 1);
	}
	static const char tbl[][3] = { "66", "67", "F2", "F3" };
	size_t tblNum = sizeof(tbl) / sizeof(tbl[0]);
	typedef std::set<std::string> StringSet;
	StringSet suf;

	pos = 0;
	for (; pos < line.size(); pos += 2) {
		bool found = false;
		for (size_t i = 0; i < tblNum; i++) {
			if (::memcmp(&line[pos], tbl[i], 2) == 0) {
				found = true;
				suf.insert(tbl[i]);
				break;
			}
		}
		if (!found) break;
	}
	std::string ret;
	for (StringSet::const_iterator i = suf.begin(), e = suf.end(); i != e; ++i) {
		ret += *i;
	}
	ret += &line[pos];
	return ret;
}

int main()
{
	std::string line;
	while (std::getline(std::cin, line)) {
		std::string normalizedLine = normalize(line);
		std::cout << normalizedLine << '\n';//std::endl;
	}
}