Convert std::sha1 to istrs. Issue #855

This commit is contained in:
Brian Anderson 2011-08-23 21:52:01 -07:00
parent bd84fbe9f3
commit 051f1ff562
3 changed files with 35 additions and 30 deletions

View File

@ -6,6 +6,7 @@ import middle::ty;
import metadata::encoder;
import middle::trans_common::crate_ctxt;
import std::str;
import std::istr;
import std::fs;
import std::vec;
import std::option;
@ -314,12 +315,14 @@ fn build_link_meta(sess: &session::session, c: &ast::crate, output: &str,
// This calculates CMH as defined above
fn crate_meta_extras_hash(sha: sha1, _crate: &ast::crate,
metas: &provided_metas) -> str {
fn len_and_str(s: &str) -> str {
ret #fmt["%u_%s", str::byte_len(s), s];
fn len_and_str(s: &istr) -> istr {
ret istr::from_estr(#fmt["%u_%s",
istr::byte_len(s),
istr::to_estr(s)]);
}
fn len_and_str_lit(l: &ast::lit) -> str {
ret len_and_str(pprust::lit_to_str(@l));
fn len_and_str_lit(l: &ast::lit) -> istr {
ret len_and_str(istr::from_estr(pprust::lit_to_str(@l)));
}
let cmh_items = attr::sort_meta_items(metas.cmh_items);
@ -329,10 +332,12 @@ fn build_link_meta(sess: &session::session, c: &ast::crate, output: &str,
let m = m_;
alt m.node {
ast::meta_name_value(key, value) {
sha.input_str(len_and_str(key));
sha.input_str(len_and_str(istr::from_estr(key)));
sha.input_str(len_and_str_lit(value));
}
ast::meta_word(name) { sha.input_str(len_and_str(name)); }
ast::meta_word(name) {
sha.input_str(len_and_str(istr::from_estr(name)));
}
ast::meta_list(_, _) {
// FIXME (#607): Implement this
fail "unimplemented meta_item variant";
@ -387,7 +392,7 @@ fn build_link_meta(sess: &session::session, c: &ast::crate, output: &str,
}
fn truncated_sha1_result(sha: sha1) -> str {
ret str::substr(sha.result_str(), 0u, 16u);
ret istr::to_estr(istr::substr(sha.result_str(), 0u, 16u));
}
@ -398,12 +403,12 @@ fn symbol_hash(tcx: ty::ctxt, sha: sha1, t: ty::t, link_meta: &link_meta) ->
// to be independent of one another in the crate.
sha.reset();
sha.input_str(link_meta.name);
sha.input_str("-");
sha.input_str(istr::from_estr(link_meta.name));
sha.input_str(~"-");
// FIXME: This wants to be link_meta.meta_hash
sha.input_str(link_meta.name);
sha.input_str("-");
sha.input_str(encoder::encoded_ty(tcx, t));
sha.input_str(istr::from_estr(link_meta.name));
sha.input_str(~"-");
sha.input_str(istr::from_estr(encoder::encoded_ty(tcx, t)));
let hash = truncated_sha1_result(sha);
// Prefix with _ so that it never blends into adjacent digits

View File

@ -10,12 +10,12 @@ type sha1 = obj {
// Provide message input as bytes
fn input(&[u8]);
// Provide message input as string
fn input_str(&str);
fn input_str(&istr);
// Read the digest as a vector of 20 bytes. After calling this no further
// input may provided until reset is called
fn result() -> [u8];
// Same as above, just a hex-string version.
fn result_str() -> str;
fn result_str() -> istr;
// Reset the sha1 state for reuse. This is called
// automatically during construction
fn reset();
@ -215,13 +215,13 @@ fn mk_sha1() -> sha1 {
st.computed = false;
}
fn input(msg: &[u8]) { add_input(st, msg); }
fn input_str(msg: &str) { add_input(st, str::bytes(msg)); }
fn input_str(msg: &istr) { add_input(st, istr::bytes(msg)); }
fn result() -> [u8] { ret mk_result(st); }
fn result_str() -> str {
fn result_str() -> istr {
let r = mk_result(st);
let s = "";
let s = ~"";
for b: u8 in r {
s += istr::to_estr(uint::to_str(b as uint, 16u));
s += uint::to_str(b as uint, 16u);
}
ret s;
}

View File

@ -5,28 +5,28 @@
use std;
import std::sha1;
import std::vec;
import std::str;
import std::istr;
#[test]
fn test() {
type test = {input: str, output: [u8]};
type test = {input: istr, output: [u8]};
fn a_million_letter_a() -> str {
fn a_million_letter_a() -> istr {
let i = 0;
let rs = "";
while i < 100000 { rs += "aaaaaaaaaa"; i += 1; }
let rs = ~"";
while i < 100000 { rs += ~"aaaaaaaaaa"; i += 1; }
ret rs;
}
// Test messages from FIPS 180-1
let fips_180_1_tests: [test] =
[{input: "abc",
[{input: ~"abc",
output:
[0xA9u8, 0x99u8, 0x3Eu8, 0x36u8, 0x47u8, 0x06u8, 0x81u8, 0x6Au8,
0xBAu8, 0x3Eu8, 0x25u8, 0x71u8, 0x78u8, 0x50u8, 0xC2u8, 0x6Cu8,
0x9Cu8, 0xD0u8, 0xD8u8, 0x9Du8]},
{input:
"abcdbcdecdefdefgefghfghighij" + "hijkijkljklmklmnlmnomnopnopq",
{input: ~"abcdbcdecdefdefgefghfghighij"
+ ~"hijkijkljklmklmnlmnomnopnopq",
output:
[0x84u8, 0x98u8, 0x3Eu8, 0x44u8, 0x1Cu8, 0x3Bu8, 0xD2u8, 0x6Eu8,
0xBAu8, 0xAEu8, 0x4Au8, 0xA1u8, 0xF9u8, 0x51u8, 0x29u8, 0xE5u8,
@ -39,12 +39,12 @@ fn test() {
// Examples from wikipedia
let wikipedia_tests: [test] =
[{input: "The quick brown fox jumps over the lazy dog",
[{input: ~"The quick brown fox jumps over the lazy dog",
output:
[0x2fu8, 0xd4u8, 0xe1u8, 0xc6u8, 0x7au8, 0x2du8, 0x28u8, 0xfcu8,
0xedu8, 0x84u8, 0x9eu8, 0xe1u8, 0xbbu8, 0x76u8, 0xe7u8, 0x39u8,
0x1bu8, 0x93u8, 0xebu8, 0x12u8]},
{input: "The quick brown fox jumps over the lazy cog",
{input: ~"The quick brown fox jumps over the lazy cog",
output:
[0xdeu8, 0x9fu8, 0x2cu8, 0x7fu8, 0xd2u8, 0x5eu8, 0x1bu8, 0x3au8,
0xfau8, 0xd3u8, 0xe8u8, 0x5au8, 0x0bu8, 0xd1u8, 0x7du8, 0x9bu8,
@ -74,11 +74,11 @@ fn test() {
// Test that it works when accepting the message in pieces
for t: test in tests {
let len = str::byte_len(t.input);
let len = istr::byte_len(t.input);
let left = len;
while left > 0u {
let take = (left + 1u) / 2u;
sh.input_str(str::substr(t.input, len - left, take));
sh.input_str(istr::substr(t.input, len - left, take));
left = left - take;
}
let out = sh.result();