-
-
Notifications
You must be signed in to change notification settings - Fork 466
Add native StringToInt64Ex #2449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -212,6 +212,16 @@ native int StringToIntEx(const char[] str, int &result, int nBase=10); | |
| */ | ||
| native int StringToInt64(const char[] str, int result[2], int nBase=10); | ||
|
|
||
| /** | ||
| * Converts a string to an int64 value. | ||
| * | ||
| * @param str String to convert. | ||
| * @param bytes Number of characters consumed. | ||
| * @param nBase Numerical base to use. 10 is default. | ||
| * @return int64 conversion of string, or 0 on failure. | ||
| */ | ||
| native int64 StringToInt64Ex(const char[] str, int &bytes=0, int nBase=10); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a little torn on the name. With the other StringTo_Ex natives, the Ex versions return the count of chars consumed and give the actual result via an out param. This does the opposite, due to it using the Ex suffix for a different reason. That said, I'm struggling to think of a better name. Something like StringToInt64Native sounds clunky. Maybe StringToI64? Anyone else have thoughts?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I'd also like to suggest Happy to go with whichever name the team prefers! |
||
|
|
||
| /** | ||
| * Converts an integer to a string. | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| #pragma semicolon 1 | ||
| #pragma newdecls required | ||
|
|
||
| #include <sourcemod> | ||
| #include <testing> | ||
|
|
||
|
|
||
| public void OnPluginStart() | ||
| { | ||
| Test_StringToInt64Ex(); | ||
| } | ||
|
|
||
|
|
||
| void Test_StringToInt64Ex() | ||
| { | ||
| SetTestContext("Test StringToInt64Ex"); | ||
|
|
||
| AssertInt64Eq("DEC 0", StringToInt64Ex("0"), 0); | ||
| AssertInt64Eq("DEC 1234567", StringToInt64Ex("1234567"), 1234567); | ||
| AssertInt64Eq("DEC 1234567654321", StringToInt64Ex("1234567654321"), 1234567654321); | ||
| AssertInt64Eq("DEC 9223372036854775807", StringToInt64Ex("9223372036854775807"), 9223372036854775807); | ||
| AssertInt64Eq("DEC -1234567", StringToInt64Ex("-1234567"), -1234567); | ||
| AssertInt64Eq("DEC -1234567654321", StringToInt64Ex("-1234567654321"), -1234567654321); | ||
| AssertInt64Eq("DEC -9223372036854775807", StringToInt64Ex("-9223372036854775807"), -9223372036854775807); | ||
|
|
||
| int bytes; | ||
|
|
||
| // bytes | ||
| AssertInt64Eq( | ||
| "result 0", | ||
| StringToInt64Ex("0", bytes), | ||
| 0); | ||
| AssertEq("bytes 0", bytes, 1); | ||
|
|
||
| AssertInt64Eq( | ||
| "result 1234567", | ||
| StringToInt64Ex("1234567", bytes), | ||
| 1234567); | ||
| AssertEq("bytes 1234567", bytes, 7); | ||
|
|
||
| AssertInt64Eq( | ||
| "result 1234567654321", | ||
| StringToInt64Ex("1234567654321", bytes), | ||
| 1234567654321); | ||
| AssertEq("bytes 1234567654321", bytes, 13); | ||
|
|
||
| AssertInt64Eq( | ||
| "result 9223372036854775807", | ||
| StringToInt64Ex("9223372036854775807", bytes), | ||
| 9223372036854775807); | ||
| AssertEq("bytes 9223372036854775807", bytes, 19); | ||
|
|
||
| AssertInt64Eq( | ||
| "result -1234567", | ||
| StringToInt64Ex("-1234567", bytes), | ||
| -1234567); | ||
| AssertEq("bytes -1234567", bytes, 8); | ||
|
|
||
| AssertInt64Eq( | ||
| "result -1234567654321", | ||
| StringToInt64Ex("-1234567654321", bytes), | ||
| -1234567654321); | ||
| AssertEq("bytes -1234567654321", bytes, 14); | ||
|
|
||
| AssertInt64Eq( | ||
| "result -9223372036854775807", | ||
| StringToInt64Ex("-9223372036854775807", bytes), | ||
| -9223372036854775807); | ||
| AssertEq("bytes -9223372036854775807", bytes, 20); | ||
|
|
||
| // Special nBase | ||
| AssertInt64Eq( | ||
| "result 10001111101110001111101110110101110110001", | ||
| StringToInt64Ex("10001111101110001111101110110101110110001", bytes, 2), | ||
| 1234567654321); | ||
| AssertEq("bytes 10001111101110001111101110110101110110001", bytes, 41); | ||
|
|
||
| AssertInt64Eq( | ||
| "result 21756175665661", | ||
| StringToInt64Ex("21756175665661", bytes, 8), | ||
| 1234567654321); | ||
| AssertEq("bytes 11F71F76BB1", bytes, 14); | ||
|
|
||
| AssertInt64Eq( | ||
| "result 11F71F76BB1", | ||
| StringToInt64Ex("11F71F76BB1", bytes, 16), | ||
| 1234567654321); | ||
| AssertEq("bytes 11F71F76BB1", bytes, 11); | ||
|
|
||
| // Orther | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor typo here. |
||
| AssertInt64Eq( | ||
| "result a1b2c3d4e5f6g7", | ||
| StringToInt64Ex("a1b2c3d4e5f6g7", bytes), | ||
| 0); | ||
| AssertEq("bytes a1b2c3d4e5f6g7", bytes, 0); | ||
|
|
||
| AssertInt64Eq( | ||
| "result 0b10101", | ||
| StringToInt64Ex("0b10101", bytes), | ||
| 0); | ||
| AssertEq("bytes 0b10101", bytes, 1); | ||
|
|
||
| AssertInt64Eq( | ||
| "result 1_234_567", | ||
| StringToInt64Ex("1_234_567", bytes), | ||
| 1); | ||
| AssertEq("bytes 1_234_567", bytes, 1); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
everything here it's pretty inconsistent with everything else, nothing here error checks, init-statement use
functionally looks good otherwise
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit confused. Could you give a more specific description of the problem?
Regarding the error checking of the
pCtx->LocalTo***part, I referenced: https://github.com/alliedmodders/sourcepawn/blob/4e87a849f092e9bb3e70e50f6a6f689e963974f0/vm/shell/shell.cpp#L127I think this way is safer, and those few condition checks won't really slow things down.
Regarding the usage of the
strtollpart, I referenced:sourcemod/core/logic/smn_string.cpp
Line 150 in 87d668a