forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemorySizeParser.cpp
More file actions
100 lines (89 loc) · 2.21 KB
/
MemorySizeParser.cpp
File metadata and controls
100 lines (89 loc) · 2.21 KB
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
91
92
93
94
95
96
97
98
99
100
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "hermes/ConsoleHost/MemorySizeParser.h"
namespace cl {
bool MemorySizeParser::parse(
cl::Option &O,
llvh::StringRef ArgName,
const std::string &Arg,
MemorySize &Val) {
const char *ArgStart = Arg.c_str();
char *End;
// Parse integer part, leaving 'End' pointing to the first non-integer char
Val.bytes = (unsigned)strtol(ArgStart, &End, 0);
if (End == ArgStart) {
return O.error("'" + Arg + "' value invalid for file size argument!");
}
enum ParserState {
SawNum,
SawPrefix,
SawPrefixPlusI,
SawWholeSpec,
Error,
};
ParserState state = SawNum;
while (1) {
if (state == Error) {
return O.error("'" + Arg + "' value invalid for file size argument!");
}
char c = *End++;
switch (c) {
case 0:
if (state == SawPrefixPlusI) {
state = Error;
} else {
return false; // No error
}
break;
case 'i': // Ignore the 'i' in KiB if people use that
if (state == SawPrefix) {
state = SawPrefixPlusI;
} else {
state = Error;
}
break;
case 'b':
case 'B': // Ignore B suffix
if (state == SawWholeSpec) {
state = Error;
} else {
state = SawWholeSpec;
}
break;
case 'g':
case 'G':
case 'm':
case 'M':
case 'k':
case 'K':
if (state != SawNum) {
state = Error;
} else {
switch (c) {
case 'g':
case 'G':
Val.bytes *= 1024 * 1024 * 1024;
break;
case 'm':
case 'M':
Val.bytes *= 1024 * 1024;
break;
case 'k':
case 'K':
Val.bytes *= 1024;
break;
}
state = SawPrefix;
}
break;
default:
// Print an error message if unrecognized character!
return O.error("'" + Arg + "' value invalid for file size argument!");
}
}
}
} // namespace cl