/ externals / xbyak / test / normalize_prefix.cpp
normalize_prefix.cpp
 1  /*
 2  	normalize prefix
 3  */
 4  #include <string>
 5  #include <set>
 6  #include <iostream>
 7  #include <memory.h>
 8  
 9  typedef unsigned char uint8_t;
10  
11  std::string normalize(std::string line)
12  {
13  	size_t pos = line.find('(');
14  	/* nasm generates byte codes containing () for xbegin, so remove it. */
15  	if (pos != std::string::npos) {
16  		line.erase(pos, 1);
17  		pos = line.find(')');
18  		if (pos == std::string::npos) {
19  			fprintf(stderr, "line error {%s}\n", line.c_str());
20  			return "";
21  		}
22  		line.erase(pos, 1);
23  	}
24  	static const char tbl[][3] = { "66", "67", "F2", "F3" };
25  	size_t tblNum = sizeof(tbl) / sizeof(tbl[0]);
26  	typedef std::set<std::string> StringSet;
27  	StringSet suf;
28  
29  	pos = 0;
30  	for (; pos < line.size(); pos += 2) {
31  		bool found = false;
32  		for (size_t i = 0; i < tblNum; i++) {
33  			if (::memcmp(&line[pos], tbl[i], 2) == 0) {
34  				found = true;
35  				suf.insert(tbl[i]);
36  				break;
37  			}
38  		}
39  		if (!found) break;
40  	}
41  	std::string ret;
42  	for (StringSet::const_iterator i = suf.begin(), e = suf.end(); i != e; ++i) {
43  		ret += *i;
44  	}
45  	ret += &line[pos];
46  	return ret;
47  }
48  
49  int main()
50  {
51  	std::string line;
52  	while (std::getline(std::cin, line)) {
53  		std::string normalizedLine = normalize(line);
54  		std::cout << normalizedLine << '\n';//std::endl;
55  	}
56  }