2022-05-18 23:28:40 +00:00
|
|
|
package pcre
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"unsafe"
|
|
|
|
|
2023-04-21 03:02:33 +00:00
|
|
|
"go.elara.ws/pcre/lib"
|
2022-05-18 23:28:40 +00:00
|
|
|
|
|
|
|
"modernc.org/libc"
|
|
|
|
"modernc.org/libc/sys/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
var pce pcreError
|
|
|
|
|
|
|
|
// pcreError is meant to be manually allocated
|
|
|
|
// for when pcre requires a pointer to store an error
|
|
|
|
// such as for Xpcre2_compile_8().
|
|
|
|
type pcreError struct {
|
|
|
|
errCode int32
|
|
|
|
errOffset lib.Tsize_t
|
|
|
|
}
|
|
|
|
|
|
|
|
// allocError manually allocates memory for the
|
|
|
|
// pcreError struct.
|
|
|
|
//
|
|
|
|
// libc.Xfree() should be called on the returned
|
|
|
|
// pointer once it is no longer needed.
|
|
|
|
func allocError(tls *libc.TLS) uintptr {
|
|
|
|
return libc.Xmalloc(tls, types.Size_t(unsafe.Sizeof(pce)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// addErrCodeOffset adds the offset of the error code
|
|
|
|
// within the pcreError struct to a given pointer
|
|
|
|
func addErrCodeOffset(p uintptr) uintptr {
|
|
|
|
ptrOffset := unsafe.Offsetof(pce.errCode)
|
|
|
|
return p + ptrOffset
|
|
|
|
}
|
|
|
|
|
|
|
|
// addErrOffsetOffset adds the offset of the error
|
|
|
|
// offset within the pcreError struct to a given pointer
|
|
|
|
func addErrOffsetOffset(p uintptr) uintptr {
|
|
|
|
offsetOffset := unsafe.Offsetof(pce.errOffset)
|
|
|
|
return p + offsetOffset
|
|
|
|
}
|
|
|
|
|
|
|
|
// ptrToError converts the given pointer to a Go error
|
|
|
|
func ptrToError(tls *libc.TLS, pe uintptr) *PcreError {
|
|
|
|
eo := *(*pcreError)(unsafe.Pointer(pe))
|
|
|
|
|
|
|
|
err := codeToError(tls, eo.errCode)
|
|
|
|
err.offset = eo.errOffset
|
2022-05-21 03:53:52 +00:00
|
|
|
err.hasOffset = true
|
2022-05-18 23:28:40 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// codeToError converts the given error code into a Go error
|
|
|
|
func codeToError(tls *libc.TLS, code int32) *PcreError {
|
|
|
|
errBuf := make([]byte, 256)
|
|
|
|
cErrBuf := uintptr(unsafe.Pointer(&errBuf[0]))
|
|
|
|
|
|
|
|
// Get the textual error message associated with the code,
|
|
|
|
// and store it in errBuf.
|
|
|
|
msgLen := lib.Xpcre2_get_error_message_8(tls, code, cErrBuf, 256)
|
|
|
|
|
2022-05-21 03:53:52 +00:00
|
|
|
return &PcreError{false, 0, string(errBuf[:msgLen])}
|
2022-05-18 23:28:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PcreError represents errors returned
|
|
|
|
// by underlying pcre2 functions.
|
|
|
|
type PcreError struct {
|
2022-05-21 03:53:52 +00:00
|
|
|
hasOffset bool
|
|
|
|
offset lib.Tsize_t
|
|
|
|
errStr string
|
2022-05-18 23:28:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns the string within the error,
|
|
|
|
// prepending the offset if it exists.
|
|
|
|
func (pe *PcreError) Error() string {
|
2022-05-21 03:53:52 +00:00
|
|
|
if !pe.hasOffset {
|
2022-05-18 23:28:40 +00:00
|
|
|
return pe.errStr
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("offset %d: %s", pe.offset, pe.errStr)
|
|
|
|
}
|