mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
hash/crc32
This commit is contained in:
parent
cec3ea5b4d
commit
b4afa964fc
@ -139,8 +139,8 @@ fn (p mut Parser) parse() {
|
|||||||
// fully qualify the module name, eg base64 to encoding.base64
|
// fully qualify the module name, eg base64 to encoding.base64
|
||||||
fq_mod := p.table.qualify_module(p.mod, p.file_path)
|
fq_mod := p.table.qualify_module(p.mod, p.file_path)
|
||||||
p.table.register_package(fq_mod)
|
p.table.register_package(fq_mod)
|
||||||
// replace "." with "__" in module name for C variable names
|
// replace "." with "_dot_" in module name for C variable names
|
||||||
p.mod = fq_mod.replace('.', '__')
|
p.mod = fq_mod.replace('.', '_dot_')
|
||||||
if p.run == .imports {
|
if p.run == .imports {
|
||||||
for p.tok == .key_import && p.peek() != .key_const {
|
for p.tok == .key_import && p.peek() != .key_const {
|
||||||
p.import_statement()
|
p.import_statement()
|
||||||
@ -1308,8 +1308,8 @@ fn (p mut Parser) name_expr() string {
|
|||||||
mut pkg := name
|
mut pkg := name
|
||||||
// must be aliased module
|
// must be aliased module
|
||||||
if name != p.mod && p.import_table.known_alias(name) {
|
if name != p.mod && p.import_table.known_alias(name) {
|
||||||
// we replaced "." with "__" in p.mod for C variable names, do same here.
|
// we replaced "." with "_dot_" in p.mod for C variable names, do same here.
|
||||||
pkg = p.import_table.resolve_alias(name).replace('.', '__')
|
pkg = p.import_table.resolve_alias(name).replace('.', '_dot_')
|
||||||
}
|
}
|
||||||
p.next()
|
p.next()
|
||||||
p.check(.dot)
|
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 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) {
|
||||||
if p.table.known_pkg(orig_name) || p.import_table.known_alias(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`')
|
p.error('undefined: `$name`')
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
59
vlib/hash/crc32/crc32.v
Normal file
59
vlib/hash/crc32/crc32.v
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
|
10
vlib/hash/crc32/crc32_test.v
Normal file
10
vlib/hash/crc32/crc32_test.v
Normal file
@ -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'
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user