Follow some advice from staticcheck.
This commit is contained in:
parent
e073f0d4ed
commit
a61eb82231
|
@ -1,7 +1,5 @@
|
||||||
package brotli
|
package brotli
|
||||||
|
|
||||||
import "math"
|
|
||||||
|
|
||||||
/* Copyright 2013 Google Inc. All Rights Reserved.
|
/* Copyright 2013 Google Inc. All Rights Reserved.
|
||||||
|
|
||||||
Distributed under MIT license.
|
Distributed under MIT license.
|
||||||
|
@ -164,163 +162,3 @@ func histogramBitCostDistanceCommand(histogram *histogramCommand, candidate *his
|
||||||
return populationCostCommand(&tmp) - candidate.bit_cost_
|
return populationCostCommand(&tmp) - candidate.bit_cost_
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Find the best 'out' histogram for each of the 'in' histograms.
|
|
||||||
When called, clusters[0..num_clusters) contains the unique values from
|
|
||||||
symbols[0..in_size), but this property is not preserved in this function.
|
|
||||||
Note: we assume that out[]->bit_cost_ is already up-to-date. */
|
|
||||||
func histogramRemapCommand(in []histogramCommand, in_size uint, clusters []uint32, num_clusters uint, out []histogramCommand, symbols []uint32) {
|
|
||||||
var i uint
|
|
||||||
for i = 0; i < in_size; i++ {
|
|
||||||
var best_out uint32
|
|
||||||
if i == 0 {
|
|
||||||
best_out = symbols[0]
|
|
||||||
} else {
|
|
||||||
best_out = symbols[i-1]
|
|
||||||
}
|
|
||||||
var best_bits float64 = histogramBitCostDistanceCommand(&in[i], &out[best_out])
|
|
||||||
var j uint
|
|
||||||
for j = 0; j < num_clusters; j++ {
|
|
||||||
var cur_bits float64 = histogramBitCostDistanceCommand(&in[i], &out[clusters[j]])
|
|
||||||
if cur_bits < best_bits {
|
|
||||||
best_bits = cur_bits
|
|
||||||
best_out = clusters[j]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
symbols[i] = best_out
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Recompute each out based on raw and symbols. */
|
|
||||||
for i = 0; i < num_clusters; i++ {
|
|
||||||
histogramClearCommand(&out[clusters[i]])
|
|
||||||
}
|
|
||||||
|
|
||||||
for i = 0; i < in_size; i++ {
|
|
||||||
histogramAddHistogramCommand(&out[symbols[i]], &in[i])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Reorders elements of the out[0..length) array and changes values in
|
|
||||||
symbols[0..length) array in the following way:
|
|
||||||
* when called, symbols[] contains indexes into out[], and has N unique
|
|
||||||
values (possibly N < length)
|
|
||||||
* on return, symbols'[i] = f(symbols[i]) and
|
|
||||||
out'[symbols'[i]] = out[symbols[i]], for each 0 <= i < length,
|
|
||||||
where f is a bijection between the range of symbols[] and [0..N), and
|
|
||||||
the first occurrences of values in symbols'[i] come in consecutive
|
|
||||||
increasing order.
|
|
||||||
Returns N, the number of unique values in symbols[]. */
|
|
||||||
|
|
||||||
var histogramReindexCommand_kInvalidIndex uint32 = math.MaxUint32
|
|
||||||
|
|
||||||
func histogramReindexCommand(out []histogramCommand, symbols []uint32, length uint) uint {
|
|
||||||
var new_index []uint32 = make([]uint32, length)
|
|
||||||
var next_index uint32
|
|
||||||
var tmp []histogramCommand
|
|
||||||
var i uint
|
|
||||||
for i = 0; i < length; i++ {
|
|
||||||
new_index[i] = histogramReindexCommand_kInvalidIndex
|
|
||||||
}
|
|
||||||
|
|
||||||
next_index = 0
|
|
||||||
for i = 0; i < length; i++ {
|
|
||||||
if new_index[symbols[i]] == histogramReindexCommand_kInvalidIndex {
|
|
||||||
new_index[symbols[i]] = next_index
|
|
||||||
next_index++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* TODO: by using idea of "cycle-sort" we can avoid allocation of
|
|
||||||
tmp and reduce the number of copying by the factor of 2. */
|
|
||||||
tmp = make([]histogramCommand, next_index)
|
|
||||||
|
|
||||||
next_index = 0
|
|
||||||
for i = 0; i < length; i++ {
|
|
||||||
if new_index[symbols[i]] == next_index {
|
|
||||||
tmp[next_index] = out[symbols[i]]
|
|
||||||
next_index++
|
|
||||||
}
|
|
||||||
|
|
||||||
symbols[i] = new_index[symbols[i]]
|
|
||||||
}
|
|
||||||
|
|
||||||
new_index = nil
|
|
||||||
for i = 0; uint32(i) < next_index; i++ {
|
|
||||||
out[i] = tmp[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp = nil
|
|
||||||
return uint(next_index)
|
|
||||||
}
|
|
||||||
|
|
||||||
func clusterHistogramsCommand(in []histogramCommand, in_size uint, max_histograms uint, out []histogramCommand, out_size *uint, histogram_symbols []uint32) {
|
|
||||||
var cluster_size []uint32 = make([]uint32, in_size)
|
|
||||||
var clusters []uint32 = make([]uint32, in_size)
|
|
||||||
var num_clusters uint = 0
|
|
||||||
var max_input_histograms uint = 64
|
|
||||||
var pairs_capacity uint = max_input_histograms * max_input_histograms / 2
|
|
||||||
var pairs []histogramPair = make([]histogramPair, (pairs_capacity + 1))
|
|
||||||
var i uint
|
|
||||||
|
|
||||||
/* For the first pass of clustering, we allow all pairs. */
|
|
||||||
for i = 0; i < in_size; i++ {
|
|
||||||
cluster_size[i] = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
for i = 0; i < in_size; i++ {
|
|
||||||
out[i] = in[i]
|
|
||||||
out[i].bit_cost_ = populationCostCommand(&in[i])
|
|
||||||
histogram_symbols[i] = uint32(i)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i = 0; i < in_size; i += max_input_histograms {
|
|
||||||
var num_to_combine uint = brotli_min_size_t(in_size-i, max_input_histograms)
|
|
||||||
var num_new_clusters uint
|
|
||||||
var j uint
|
|
||||||
for j = 0; j < num_to_combine; j++ {
|
|
||||||
clusters[num_clusters+j] = uint32(i + j)
|
|
||||||
}
|
|
||||||
|
|
||||||
num_new_clusters = histogramCombineCommand(out, cluster_size, histogram_symbols[i:], clusters[num_clusters:], pairs, num_to_combine, num_to_combine, max_histograms, pairs_capacity)
|
|
||||||
num_clusters += num_new_clusters
|
|
||||||
}
|
|
||||||
{
|
|
||||||
/* For the second pass, we limit the total number of histogram pairs.
|
|
||||||
After this limit is reached, we only keep searching for the best pair. */
|
|
||||||
var max_num_pairs uint = brotli_min_size_t(64*num_clusters, (num_clusters/2)*num_clusters)
|
|
||||||
if pairs_capacity < (max_num_pairs + 1) {
|
|
||||||
var _new_size uint
|
|
||||||
if pairs_capacity == 0 {
|
|
||||||
_new_size = max_num_pairs + 1
|
|
||||||
} else {
|
|
||||||
_new_size = pairs_capacity
|
|
||||||
}
|
|
||||||
var new_array []histogramPair
|
|
||||||
for _new_size < (max_num_pairs + 1) {
|
|
||||||
_new_size *= 2
|
|
||||||
}
|
|
||||||
new_array = make([]histogramPair, _new_size)
|
|
||||||
if pairs_capacity != 0 {
|
|
||||||
copy(new_array, pairs[:pairs_capacity])
|
|
||||||
}
|
|
||||||
|
|
||||||
pairs = new_array
|
|
||||||
pairs_capacity = _new_size
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Collapse similar histograms. */
|
|
||||||
num_clusters = histogramCombineCommand(out, cluster_size, histogram_symbols, clusters, pairs, num_clusters, in_size, max_histograms, max_num_pairs)
|
|
||||||
}
|
|
||||||
|
|
||||||
pairs = nil
|
|
||||||
cluster_size = nil
|
|
||||||
|
|
||||||
/* Find the optimal map from original histograms to the final ones. */
|
|
||||||
histogramRemapCommand(in, in_size, clusters, num_clusters, out, histogram_symbols)
|
|
||||||
|
|
||||||
clusters = nil
|
|
||||||
|
|
||||||
/* Convert the context map to a canonical form. */
|
|
||||||
*out_size = histogramReindexCommand(out, histogram_symbols, in_size)
|
|
||||||
}
|
|
||||||
|
|
|
@ -604,7 +604,7 @@ emit_commands:
|
||||||
assert(candidate < ip)
|
assert(candidate < ip)
|
||||||
|
|
||||||
table[hash] = int(ip - base_ip)
|
table[hash] = int(ip - base_ip)
|
||||||
if !(!isMatch5(in[ip:], in[candidate:])) {
|
if isMatch5(in[ip:], in[candidate:]) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
31
decode.go
31
decode.go
|
@ -50,15 +50,6 @@ const (
|
||||||
decoderErrorUnreachable = -31
|
decoderErrorUnreachable = -31
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
|
||||||
* The value of the last error code, negative integer.
|
|
||||||
*
|
|
||||||
* All other error code values are in the range from ::lastErrorCode
|
|
||||||
* to @c -1. There are also 4 other possible non-error codes @c 0 .. @c 3 in
|
|
||||||
* ::BrotliDecoderErrorCode enumeration.
|
|
||||||
*/
|
|
||||||
const lastErrorCode = decoderErrorUnreachable
|
|
||||||
|
|
||||||
/** Options to be used with ::BrotliDecoderSetParameter. */
|
/** Options to be used with ::BrotliDecoderSetParameter. */
|
||||||
const (
|
const (
|
||||||
decoderParamDisableRingBufferReallocation = 0
|
decoderParamDisableRingBufferReallocation = 0
|
||||||
|
@ -81,28 +72,6 @@ var kCodeLengthPrefixLength = [16]byte{2, 2, 2, 3, 2, 2, 2, 4, 2, 2, 2, 3, 2, 2,
|
||||||
|
|
||||||
var kCodeLengthPrefixValue = [16]byte{0, 4, 3, 2, 0, 4, 3, 1, 0, 4, 3, 2, 0, 4, 3, 5}
|
var kCodeLengthPrefixValue = [16]byte{0, 4, 3, 2, 0, 4, 3, 1, 0, 4, 3, 2, 0, 4, 3, 5}
|
||||||
|
|
||||||
func decoderSetParameter(state *Reader, p int, value uint32) bool {
|
|
||||||
if state.state != stateUninited {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
switch p {
|
|
||||||
case decoderParamDisableRingBufferReallocation:
|
|
||||||
if !(value == 0) {
|
|
||||||
state.canny_ringbuffer_allocation = 0
|
|
||||||
} else {
|
|
||||||
state.canny_ringbuffer_allocation = 1
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
|
|
||||||
case decoderParamLargeWindow:
|
|
||||||
state.large_window = (!(value == 0))
|
|
||||||
return true
|
|
||||||
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Saves error code and converts it to BrotliDecoderResult. */
|
/* Saves error code and converts it to BrotliDecoderResult. */
|
||||||
func saveErrorCode(s *Reader, e int) int {
|
func saveErrorCode(s *Reader, e int) int {
|
||||||
s.error_code = int(e)
|
s.error_code = int(e)
|
||||||
|
|
4
http.go
4
http.go
|
@ -180,8 +180,8 @@ func init() {
|
||||||
var t octetType
|
var t octetType
|
||||||
isCtl := c <= 31 || c == 127
|
isCtl := c <= 31 || c == 127
|
||||||
isChar := 0 <= c && c <= 127
|
isChar := 0 <= c && c <= 127
|
||||||
isSeparator := strings.IndexRune(" \t\"(),/:;<=>?@[]\\{}", rune(c)) >= 0
|
isSeparator := strings.ContainsRune(" \t\"(),/:;<=>?@[]\\{}", rune(c))
|
||||||
if strings.IndexRune(" \t\r\n", rune(c)) >= 0 {
|
if strings.ContainsRune(" \t\r\n", rune(c)) {
|
||||||
t |= isSpace
|
t |= isSpace
|
||||||
}
|
}
|
||||||
if isChar && !isCtl && !isSeparator {
|
if isChar && !isCtl && !isSeparator {
|
||||||
|
|
Loading…
Reference in New Issue