Skip to content

Commit 78eb6cc

Browse files
committed
[GR-11066] Time#strftime fails if the format is a BINARY String.
PullRequest: truffleruby/920
2 parents a4279dc + 24d55f4 commit 78eb6cc

File tree

6 files changed

+13
-41
lines changed

6 files changed

+13
-41
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Bug fixes:
44

55
* Fixed `Symbol#match` returning `MatchData` (#1706).
6+
* Allow `Time#strftime` to be called with binary format strings.
67

78
Compatibility:
89

src/main/java/org/truffleruby/core/rope/RopeOperations.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public static Rope encodeAscii(String value, Encoding encoding) {
136136
}
137137

138138
public static byte[] encodeAsciiBytes(String value) {
139-
assert StringOperations.isASCIIOnly(value);
139+
assert StringOperations.isAsciiOnly(value);
140140

141141
final byte[] bytes = new byte[value.length()];
142142

src/main/java/org/truffleruby/core/string/StringOperations.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
3737
import com.oracle.truffle.api.object.DynamicObject;
3838
import org.jcodings.Encoding;
39+
import org.jcodings.specific.ASCIIEncoding;
3940
import org.truffleruby.Layouts;
4041
import org.truffleruby.RubyContext;
4142
import org.truffleruby.core.array.ArrayOperations;
@@ -69,9 +70,13 @@ public static int clampExclusiveIndex(DynamicObject string, int index) {
6970
public static byte[] encodeBytes(String value, Encoding encoding) {
7071
// Taken from org.jruby.RubyString#encodeByteList.
7172

73+
if (encoding == ASCIIEncoding.INSTANCE && !isAsciiOnly(value)) {
74+
throw new UnsupportedOperationException(
75+
StringUtils.format("Can't convert Java String (%s) to Ruby BINARY String because it contains non-ASCII characters", value));
76+
}
77+
7278
Charset charset = encoding.getCharset();
7379

74-
// if null charset, fall back on Java default charset
7580
if (charset == null) {
7681
throw new UnsupportedOperationException("Cannot find Charset to encode " + value + " with " + encoding);
7782
}
@@ -109,7 +114,7 @@ public static Encoding encoding(DynamicObject string) {
109114
return rope(string).getEncoding();
110115
}
111116

112-
public static boolean isASCIIOnly(String string) {
117+
public static boolean isAsciiOnly(String string) {
113118
for (int i = 0; i < string.length(); i++) {
114119
int c = string.charAt(i);
115120
if (!Encoding.isAscii(c)) {

src/main/java/org/truffleruby/core/symbol/SymbolTable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public DynamicObject getSymbol(String string) {
7979
}
8080

8181
final Rope rope;
82-
if (StringOperations.isASCIIOnly(string)) {
82+
if (StringOperations.isAsciiOnly(string)) {
8383
rope = RopeOperations.encodeAscii(string, USASCIIEncoding.INSTANCE);
8484
} else {
8585
rope = StringOperations.encodeRope(string, UTF8Encoding.INSTANCE);

src/main/java/org/truffleruby/core/time/RubyDateFormatter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import org.jcodings.Encoding;
4646
import org.jcodings.specific.ASCIIEncoding;
4747
import org.truffleruby.RubyContext;
48-
import org.truffleruby.core.encoding.EncodingManager;
4948
import org.truffleruby.core.rope.Rope;
5049
import org.truffleruby.core.rope.RopeBuilder;
5150
import org.truffleruby.core.rope.RopeOperations;
@@ -522,7 +521,7 @@ public static RopeBuilder formatToRopeBuilder(List<Token> compiledPattern, Zoned
522521
// reset formatter
523522
formatter = RubyTimeOutputFormatter.DEFAULT_FORMATTER;
524523

525-
toAppendTo.append(output.getBytes(EncodingManager.charsetForEncoding(toAppendTo.getEncoding())));
524+
toAppendTo.append(StringOperations.encodeBytes(output, toAppendTo.getEncoding()));
526525
}
527526

528527
return toAppendTo;
Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,5 @@
1-
exclude :test_completion, "needs investigation"
2-
exclude :test_completion_with_different_timezone, "needs investigation"
3-
exclude :test_encode_httpdate, "needs investigation"
4-
exclude :test_encode_rfc2822, "needs investigation"
5-
exclude :test_encode_xmlschema, "needs investigation"
6-
exclude :test_huge_precision, "needs investigation"
7-
exclude :test_invalid, "needs investigation"
8-
exclude :test_nsec, "needs investigation"
9-
exclude :test_parse_fraction, "needs investigation"
10-
exclude :test_parse_leap_second, "needs investigation"
11-
exclude :test_rfc2616, "needs investigation"
12-
exclude :test_rfc2822, "needs investigation"
13-
exclude :test_rfc2822_leap_second, "needs investigation"
14-
exclude :test_rfc2822_utc_roundtrip_summer, "needs investigation"
15-
exclude :test_rfc2822_utc_roundtrip_winter, "needs investigation"
16-
exclude :test_rfc3339, "needs investigation"
17-
exclude :test_rfc822, "needs investigation"
18-
exclude :test_ruby_talk_152866, "needs investigation"
19-
exclude :test_strptime, "needs investigation"
20-
exclude :test_strptime_Ymd_z, "needs investigation"
21-
exclude :test_strptime_empty, "needs investigation"
22-
exclude :test_strptime_s_z, "needs investigation"
23-
exclude :test_xmlschema, "needs investigation"
24-
exclude :test_xmlschema_fraction, "needs investigation"
25-
exclude :test_xmlschema_leap_second, "needs investigation"
26-
exclude :test_zone_0000, "needs investigation"
27-
exclude :test_iso8601, "needs investigation"
28-
exclude :test_iso8601_alias, "needs investigation"
291
exclude :test_iso8601_encode, "needs investigation"
30-
exclude :test_iso8601_nsec, "needs investigation"
312
exclude :test_strptime_s_N, "needs investigation"
32-
exclude :test_xmlschema_alias, "needs investigation"
333
exclude :test_xmlschema_encode, "needs investigation"
34-
exclude :test_xmlschema_nsec, "needs investigation"
35-
exclude :test_iso8601_leap_second, "needs investigation"
36-
exclude :test_parse_offset_hour_minute_second, "needs investigation"
37-
exclude :test_strptime_j, "needs investigation"
38-
exclude :test_parse_with_various_object, "needs investigation"
4+
exclude :test_iso8601, "needs investigation"
5+
exclude :test_xmlschema, "needs investigation"

0 commit comments

Comments
 (0)