Files
go-dpkg/deb822/deb822.go
Matheus Sampaio Queiroga 8cf5fcbfc3
All checks were successful
Golang test / go-test (pull_request) Successful in 21s
Golang test / go-test (push) Successful in 21s
Refactor deb822 package: Remove deprecated files and implement new encoding/decoding logic
- Deleted old deb822_encode.go and deb822_types.go files.
- Introduced new deb822_encode.go with improved Marshall function and Writer struct for encoding.
- Added deb822_decode.go for decoding functionality with enhanced error handling.
- Created deb822_rawdata.go to define RawData type for handling raw deb822 values.
- Implemented Description type in deb822/datatype/description.go for structured description handling.
- Updated Package struct in dpkg/header.go to use new Description type.
- Refactored UnmarshalBinary method in dpkg/header.go to utilize new deb822 decoding logic.
- Added comprehensive tests for encoding and decoding in deb822_encode_test.go and deb822_decode_test.go.
- Removed internal scanner package as it was no longer needed.

Signed-off-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com>
2025-06-22 22:05:41 -03:00

66 lines
1.4 KiB
Go

package deb822
import (
"errors"
"iter"
"strings"
"unicode"
)
var (
ErrInvalidDeb822 = errors.New("invalid deb822, check file if is valid or in deb822 style")
ErrNextDeb822 = errors.New("current deb822 have multiples of deb822")
)
// Format value string to description
func FromDescription(value string) (lines [2]string) {
n2 := strings.SplitN(value, "\n", 2)
if len(n2) >= 1 {
lines[0] = n2[0]
}
if len(n2) >= 2 {
lines[1] = n2[1]
secLines := strings.Split(strings.TrimLeftFunc(lines[1], unicode.IsSpace), "\n")
switch len(secLines) {
case 0:
case 1:
secLines[0] = strings.TrimLeftFunc(secLines[0], unicode.IsSpace)
default:
cut := strings.IndexFunc(secLines[1], func(r rune) bool { return !unicode.IsSpace(r) })
for i := range secLines {
if i == 0 {
continue
}
secLines[i] = secLines[i][cut:]
if secLines[i] == "." {
secLines[i] = ""
}
}
}
lines[1] = strings.Join(secLines, "\n")
}
return
}
func ToSlice(data string) iter.Seq[string] {
return func(yield func(string) bool) {
lines := strings.Split(strings.TrimSpace(data), "\n")
var cut int
if len(lines) > 1 {
cut = max(0, strings.IndexFunc(lines[1], func(r rune) bool { return !unicode.IsSpace(r) }))
}
for i := range lines {
if i != 0 && len(lines[i]) >= cut {
lines[i] = lines[i][cut:]
if lines[i] == "." {
lines[i] = ""
}
}
if !yield(lines[i]) {
break
}
}
}
}