Skip to content

Commit 6b81c24

Browse files
authored
Rollup merge of rust-lang#107284 - notriddle:notriddle/plus, r=jsha
rustdoc: use smarter encoding for playground URL The old way would compress okay with DEFLATE, but this version makes uncompressed docs smaller, which matters for memory usage and stuff like `cargo doc`. Try it out: <https://play.rust-lang.org/?code=fn+main()+{%0Alet+mut+v+=+Vec::new();%0Av.push(1+/+1);%0Aprintln!(%22{}%22,+v[0]);%0A}> In local testing, this change shrinks sample pages by anywhere between 5.0% and 0.044% $ du -b after.dir/std/vec/struct.Vec.html before.dir/std/vec/struct.Vec.html 753023 after.dir/std/vec/struct.Vec.html 781842 before.dir/std/vec/struct.Vec.html 100*((753023-781842)/781842)=-3.7 $ du -b after.dir/std/num/struct.Wrapping.html before.dir/std/num/struct.Wrapping.html 3189989 after.dir/std/num/struct.Wrapping.html 3204351 before.dir/std/num/struct.Wrapping.html 100*((3189989-3204351)/3204351)=-0.044 $ du -b after.dir/std/keyword.match.html before.dir/std/keyword.match.html 8067 after.dir/std/keyword.match.html 8495 before.dir/std/keyword.match.html 100*((8067-8495)/8495)=-5.0 Gzipped tarball sizes seem shrunk, but not by much. du -s before.tar.gz after.tar.gz 69600 before.tar.gz 69492 after.tar.gz 100*((69492-69600)/69600)=-0.16
2 parents 6c440bf + 0db0419 commit 6b81c24

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

src/librustdoc/html/markdown.rs

+35-1
Original file line numberDiff line numberDiff line change
@@ -296,25 +296,59 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
296296
let channel = if test.contains("#![feature(") { "&amp;version=nightly" } else { "" };
297297

298298
// These characters don't need to be escaped in a URI.
299-
// FIXME: use a library function for percent encoding.
299+
// See https://url.spec.whatwg.org/#query-percent-encode-set
300+
// and https://url.spec.whatwg.org/#urlencoded-parsing
300301
fn dont_escape(c: u8) -> bool {
301302
(b'a' <= c && c <= b'z')
302303
|| (b'A' <= c && c <= b'Z')
303304
|| (b'0' <= c && c <= b'9')
304305
|| c == b'-'
305306
|| c == b'_'
306307
|| c == b'.'
308+
|| c == b','
307309
|| c == b'~'
308310
|| c == b'!'
309311
|| c == b'\''
310312
|| c == b'('
311313
|| c == b')'
314+
|| c == b'['
315+
|| c == b']'
316+
|| c == b'{'
317+
|| c == b'}'
312318
|| c == b'*'
319+
|| c == b'/'
320+
|| c == b'|'
321+
|| c == b'^'
322+
|| c == b'\\'
323+
|| c == b';'
324+
|| c == b':'
325+
|| c == b'?'
326+
// While these would be encoded if we used the URL
327+
// serializer algorithm, they don't actually need to be
328+
// encoded for the [query mode parser] to give the correct
329+
// result. The HTML [attribute parser] does the right
330+
// thing with them as well, as long as the attribute is
331+
// quoted (which it is).
332+
//
333+
// [query mode parser]: https://url.spec.whatwg.org/#query-state
334+
// [attribute parser]: https://html.spec.whatwg.org/#attribute-value-(double-quoted)-state
335+
|| c == b'<'
336+
|| c == b'>'
337+
// As described in urlencoded-parsing, the
338+
// first `=` is the one that separates key from
339+
// value. Following `=`s are part of the value.
340+
|| c == b'='
313341
}
314342
let mut test_escaped = String::new();
315343
for b in test.bytes() {
316344
if dont_escape(b) {
317345
test_escaped.push(char::from(b));
346+
} else if b == b' ' {
347+
// URL queries are decoded with + replaced with SP
348+
test_escaped.push('+');
349+
} else if b == b'%' {
350+
test_escaped.push('%');
351+
test_escaped.push('%');
318352
} else {
319353
write!(test_escaped, "%{:02X}", b).unwrap();
320354
}

tests/rustdoc/playground-arg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
pub fn dummy() {}
1111

1212
// ensure that `extern crate foo;` was inserted into code snips automatically:
13-
// @matches foo/index.html '//a[@class="test-arrow"][@href="https://example.com/?code=%23!%5Ballow(unused)%5D%0Aextern%20crate%20r%23foo%3B%0Afn%20main()%20%7B%0Ause%20foo%3A%3Adummy%3B%0Adummy()%3B%0A%7D&edition=2015"]' "Run"
13+
// @matches foo/index.html '//a[@class="test-arrow"][@href="https://example.com/?code=%23![allow(unused)]%0Aextern+crate+r%23foo;%0Afn+main()+{%0Ause+foo::dummy;%0Adummy();%0A}&edition=2015"]' "Run"

tests/rustdoc/playground.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@
2222
//! }
2323
//! ```
2424
25-
// @matches foo/index.html '//a[@class="test-arrow"][@href="https://www.example.com/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0A%20%20%20%20println!(%22Hello%2C%20world!%22)%3B%0A%7D&edition=2015"]' "Run"
26-
// @matches foo/index.html '//a[@class="test-arrow"][@href="https://www.example.com/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Aprintln!(%22Hello%2C%20world!%22)%3B%0A%7D&edition=2015"]' "Run"
27-
// @matches foo/index.html '//a[@class="test-arrow"][@href="https://www.example.com/?code=%23!%5Ballow(unused)%5D%0A%23!%5Bfeature(something)%5D%0A%0Afn%20main()%20%7B%0A%20%20%20%20println!(%22Hello%2C%20world!%22)%3B%0A%7D&version=nightly&edition=2015"]' "Run"
25+
// @matches foo/index.html '//a[@class="test-arrow"][@href="https://www.example.com/?code=%23![allow(unused)]%0Afn+main()+{%0Aprintln!(%22Hello,+world!%22);%0A}&edition=2015"]' "Run"
26+
// @matches foo/index.html '//a[@class="test-arrow"][@href="https://www.example.com/?code=%23![allow(unused)]%0Afn+main()+{%0A++++println!(%22Hello,+world!%22);%0A}&edition=2015"]' "Run"
27+
// @matches foo/index.html '//a[@class="test-arrow"][@href="https://www.example.com/?code=%23![allow(unused)]%0A%23![feature(something)]%0A%0Afn+main()+{%0A++++println!(%22Hello,+world!%22);%0A}&version=nightly&edition=2015"]' "Run"

0 commit comments

Comments
 (0)