diff --git a/compiler/parser.v b/compiler/parser.v index 2fbfd5e29d..d7affbd86c 100644 --- a/compiler/parser.v +++ b/compiler/parser.v @@ -139,8 +139,8 @@ fn (p mut Parser) parse() { // fully qualify the module name, eg base64 to encoding.base64 fq_mod := p.table.qualify_module(p.mod, p.file_path) p.table.register_package(fq_mod) - // replace "." with "__" in module name for C variable names - p.mod = fq_mod.replace('.', '__') + // replace "." with "_dot_" in module name for C variable names + p.mod = fq_mod.replace('.', '_dot_') if p.run == .imports { for p.tok == .key_import && p.peek() != .key_const { p.import_statement() @@ -1308,8 +1308,8 @@ fn (p mut Parser) name_expr() string { mut pkg := name // must be aliased module if name != p.mod && p.import_table.known_alias(name) { - // we replaced "." with "__" in p.mod for C variable names, do same here. - pkg = p.import_table.resolve_alias(name).replace('.', '__') + // we replaced "." with "_dot_" in p.mod for C variable names, do same here. + pkg = p.import_table.resolve_alias(name).replace('.', '_dot_') } p.next() p.check(.dot) @@ -1435,7 +1435,7 @@ fn (p mut Parser) name_expr() string { // If orig_name is a pkg, then printing undefined: `pkg` tells us nothing // if p.table.known_pkg(orig_name) { if p.table.known_pkg(orig_name) || p.import_table.known_alias(orig_name) { - name = name.replace('__', '.') + name = name.replace('__', '.').replace('_dot_', '.') p.error('undefined: `$name`') } else { diff --git a/vlib/hash/crc32/crc32.v b/vlib/hash/crc32/crc32.v new file mode 100644 index 0000000000..606f839abe --- /dev/null +++ b/vlib/hash/crc32/crc32.v @@ -0,0 +1,59 @@ +// Copyright (c) 2019 Alexander Medvednikov. All rights reserved. +// Use of this source code is governed by an MIT license +// that can be found in the LICENSE file. + +// This is a very basic crc32 implementation +// at the moment with no architecture optimizations +module crc32 + +// polynomials +const ( + IEEE = 0xedb88320 + Castagnoli = 0x82f63b78 + Koopman = 0xeb31d82e +) + +struct Crc32 { +mut: + table []u32 +} + +fn(c mut Crc32) generate_table(poly int) { + for i := 0; i < 256; i++ { + mut crc := u32(i) + for j := 0; j < 8; j++ { + if crc&u32(1) == u32(1) { + crc = u32((crc >> u32(1)) ^ poly) + } else { + crc >>= u32(1) + } + } + c.table << crc + } +} + +fn(c &Crc32) sum_32(s string) u32 { + mut crc := ~u32(0) + for i := 0; i < s.len; i++ { + crc = c.table[byte(crc)^s[i]] ^ u32(crc >> u32(8)) + } + return ~crc +} + +pub fn(c &Crc32) checksum(s string) u32 { + return c.sum_32(s) +} + +// pass the polinomial to use +pub fn new(poly int) *Crc32 { + mut c := &Crc32{} + c.generate_table(poly) + return c +} + +// calculate crc32 using IEEE +pub fn sum(s string) u32 { + mut c := new(IEEE) + return c.sum_32(s) +} + diff --git a/vlib/hash/crc32/crc32_test.v b/vlib/hash/crc32/crc32_test.v new file mode 100644 index 0000000000..4aad09c614 --- /dev/null +++ b/vlib/hash/crc32/crc32_test.v @@ -0,0 +1,10 @@ +import hash.crc32 + +fn test_hash_crc32() { + assert crc32.sum('testing crc32') == u32(1212124400) + assert crc32.sum('testing crc32').hex() == '0x483f8cf0' + + c := crc32.new(crc32.IEEE) + assert c.checksum('testing crc32 again') == u32(1420327025) + assert c.checksum('testing crc32 again').hex() == '0x54a87871' +} \ No newline at end of file