mirror of
https://github.com/joyieldInc/predixy.git
synced 2026-02-05 01:42:24 +08:00
Parse signed integers in string utils
This commit is contained in:
parent
560dbe7fc7
commit
37e8bbd86e
20
src/String.h
20
src/String.h
@ -121,18 +121,20 @@ public:
|
||||
}
|
||||
bool toInt(int& v) const
|
||||
{
|
||||
if (mLen <= 0 || !mDat) {
|
||||
return false;
|
||||
}
|
||||
v = 0;
|
||||
int i = 0;
|
||||
int sign = 1;
|
||||
if (i > 0) {
|
||||
if (mDat[0] == '+') {
|
||||
++i;
|
||||
} else if (mDat[0] == '+') {
|
||||
sign = -1;
|
||||
++i;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
if (mDat[0] == '+') {
|
||||
++i;
|
||||
} else if (mDat[0] == '-') {
|
||||
sign = -1;
|
||||
++i;
|
||||
}
|
||||
if (i >= mLen) {
|
||||
return false;
|
||||
}
|
||||
for ( ; i < mLen; ++i) {
|
||||
if (mDat[i] >= '0' && mDat[i] <= '9') {
|
||||
|
||||
@ -149,6 +149,7 @@ TESTS=(
|
||||
"test/logunit_vsnprintf.py"
|
||||
"test/logger_thread_init.py"
|
||||
"test/logger_unbounded_logunit.py"
|
||||
"test/string_to_int.py"
|
||||
"test/pubsub_long_name.py"
|
||||
"test/pubsub_large_message.py"
|
||||
"test/transaction_forbid.py"
|
||||
|
||||
49
test/string_to_int.cpp
Normal file
49
test/string_to_int.cpp
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Verify String::toInt handles signed values.
|
||||
*/
|
||||
|
||||
#include "../src/String.h"
|
||||
|
||||
int main() {
|
||||
int v = 0;
|
||||
{
|
||||
String s("+1", 2);
|
||||
bool ok = s.toInt(v);
|
||||
if (!ok || v != 1) {
|
||||
fprintf(stderr, "toInt(+1) ok=%d v=%d len=%d dat=%c%c\n",
|
||||
(int)ok, v, s.length(),
|
||||
s.data() ? s.data()[0] : '?',
|
||||
s.data() ? s.data()[1] : '?');
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
{
|
||||
bool ok = String("-1", 2).toInt(v);
|
||||
if (!ok || v != -1) {
|
||||
fprintf(stderr, "toInt(-1) ok=%d v=%d\n", (int)ok, v);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
{
|
||||
bool ok = String("0", 1).toInt(v);
|
||||
if (!ok || v != 0) {
|
||||
fprintf(stderr, "toInt(0) ok=%d v=%d\n", (int)ok, v);
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
{
|
||||
bool ok = String("+", 1).toInt(v);
|
||||
if (ok) {
|
||||
fprintf(stderr, "toInt(+) ok=%d v=%d\n", (int)ok, v);
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
{
|
||||
bool ok = String("-", 1).toInt(v);
|
||||
if (ok) {
|
||||
fprintf(stderr, "toInt(-) ok=%d v=%d\n", (int)ok, v);
|
||||
return 5;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
44
test/string_to_int.py
Normal file
44
test/string_to_int.py
Normal file
@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# Build and run the String::toInt sign handling test.
|
||||
#
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
from test_util import parse_args, exit_with_result
|
||||
|
||||
|
||||
def run_test(project_root):
|
||||
src = os.path.join(project_root, "test", "string_to_int.cpp")
|
||||
if not os.path.exists(src):
|
||||
print("FAIL: string_to_int.cpp not found")
|
||||
return False
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
exe = os.path.join(tmp, "string_to_int")
|
||||
cmd = [
|
||||
"g++",
|
||||
"-std=c++11",
|
||||
src,
|
||||
"-o",
|
||||
exe,
|
||||
]
|
||||
try:
|
||||
subprocess.check_call(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
except Exception as exc:
|
||||
print("FAIL: compile string_to_int:", exc)
|
||||
return False
|
||||
try:
|
||||
subprocess.check_call([exe], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
except subprocess.CalledProcessError as exc:
|
||||
print("FAIL: string_to_int returned", exc.returncode)
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
_ = parse_args("String toInt test")
|
||||
root = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
||||
success = run_test(root)
|
||||
exit_with_result(success, "string toInt", "string toInt")
|
||||
Loading…
Reference in New Issue
Block a user