diff options
author | brandjon <brandjon@google.com> | 2018-03-26 12:41:12 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-03-26 12:42:52 -0700 |
commit | 7a45873dcdebc3e78cc7f26402d533ef9101106b (patch) | |
tree | 0c3674d35c27654161d54020239e56cb021d8417 | |
parent | 61b40750f733d8fb47d50de6965ddf7a3f2dbe66 (diff) |
Fix incorrect int() example
Also add tests for leading "+" in int(<string>) form.
RELNOTES: None
PiperOrigin-RevId: 190507678
3 files changed, 17 insertions, 9 deletions
diff --git a/site/docs/skylark/spec.md b/site/docs/skylark/spec.md index 53c0a5d482..5eedb45dfc 100644 --- a/site/docs/skylark/spec.md +++ b/site/docs/skylark/spec.md @@ -249,7 +249,7 @@ else: The Skylark integer type represents integers. Its [type](#type) is `"int"`. Integers may be positive or negative. The precision is implementation-dependent. -It is a dynamic error if a result is outside that the supported range. +It is a dynamic error if a result is outside the supported range. Integers are totally ordered; comparisons follow mathematical tradition. @@ -2327,12 +2327,15 @@ implies `hash(x) == hash(y)`. `int(x[, base])` interprets its argument as an integer. -If x is an `int`, the result is x. -If x is a `bool`, the result is 0 for `False` or 1 for `True`. +If `x` is an `int`, the result is `x`. +If `x` is a `bool`, the result is 0 for `False` or 1 for `True`. -If x is a string, it is interpreted as an integer using the base argument -(default is `10`). If base is `0`, x is interpreted like an integer literal, the -base being inferred from an optional base prefix (for example `0o` or `0x`). +If `x` is a string, it is interpreted as a sequence of digits in the specified +base, decimal by default. If `base` is zero, `x` is interpreted like an integer +literal, the base being inferred from an optional base marker such as `0b`, +`0o`, or `0x` preceding the first digit. These markers may also be used if +`base` is the corresponding base. Irrespective of base, the string may start +with an optional `+` or `-` sign indicating the sign of the result. ```python int("21") # 21 diff --git a/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java b/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java index 6f5e519a88..0597ebf1c9 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java @@ -1719,10 +1719,12 @@ public class MethodLibrary { + "<p>Examples:" + "<pre class=\"language-python\">" + "int(\"123\") == 123\n" - + "int(\" -123 \") == -123\n" + + "int(\"-123\") == -123\n" + + "int(\"+123\") == 123\n" + "int(\"FF\", 16) == 255\n" + + "int(\"0xFF\", 16) == 255\n" + "int(\"10\", 0) == 10\n" - + "int(\"0x10\", 0) == 16" + + "int(\"-0x10\", 0) == -16" + "</pre>", parameters = { @Param(name = "x", type = Object.class, doc = "The string to convert."), diff --git a/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java b/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java index 495978c0c3..ccf170fb47 100644 --- a/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java +++ b/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java @@ -986,7 +986,9 @@ public class MethodLibraryTest extends EvaluationTestCase { .testStatement("int('2147483647')", 2147483647) .testStatement("int('-2147483648')", -2147483648) // Leading zero allowed when not using base = 0. - .testStatement("int('016')", 16); + .testStatement("int('016')", 16) + // Leading plus sign allowed for strings. + .testStatement("int('+42')", 42); } @Test @@ -1036,6 +1038,7 @@ public class MethodLibraryTest extends EvaluationTestCase { new BothModesTest() .testStatement("int('0b11', 0)", 3) .testStatement("int('-0b11', 0)", -3) + .testStatement("int('+0b11', 0)", 3) .testStatement("int('0B11', 2)", 3) .testStatement("int('0o11', 0)", 9) .testStatement("int('0O11', 8)", 9) |