Initial QSfera import

This commit is contained in:
Курнат Андрей
2026-06-07 10:20:04 +03:00
commit 2315f25754
16485 changed files with 4826827 additions and 0 deletions
+164
View File
@@ -0,0 +1,164 @@
package checkfn
import (
"bytes"
"fmt"
"reflect"
"regexp"
"strings"
)
// IsNil value check
func IsNil(v any) bool {
if v == nil {
return true
}
rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
return rv.IsNil()
default:
return false
}
}
// IsSimpleKind kind in: string, bool, intX, uintX, floatX
func IsSimpleKind(k reflect.Kind) bool {
if reflect.String == k {
return true
}
return k > reflect.Invalid && k <= reflect.Float64
}
// IsEqual determines if two objects are considered equal.
//
// TIP: cannot compare function type
func IsEqual(src, dst any) bool {
if src == nil || dst == nil {
return src == dst
}
bs1, ok := src.([]byte)
if !ok {
return reflect.DeepEqual(src, dst)
}
bs2, ok := dst.([]byte)
if !ok {
return false
}
if bs1 == nil || bs2 == nil {
return bs1 == nil && bs2 == nil
}
return bytes.Equal(bs1, bs2)
}
// Contains try loop over the data check if the data includes the element.
//
// data allow types: string, map, array, slice
//
// map - check key exists
// string - check sub-string exists
// array,slice - check sub-element exists
//
// Returns:
// - valid: data is valid
// - found: element was found
//
// return (false, false) if impossible.
// return (true, false) if element was not found.
// return (true, true) if element was found.
func Contains(data, elem any) (valid, found bool) {
if data == nil {
return false, false
}
dataRv := reflect.ValueOf(data)
dataRt := reflect.TypeOf(data)
dataKind := dataRt.Kind()
// string
if dataKind == reflect.String {
return true, strings.Contains(dataRv.String(), fmt.Sprint(elem))
}
// map
if dataKind == reflect.Map {
mapKeys := dataRv.MapKeys()
for i := 0; i < len(mapKeys); i++ {
if IsEqual(mapKeys[i].Interface(), elem) {
return true, true
}
}
return true, false
}
// array, slice - other return false
if dataKind != reflect.Slice && dataKind != reflect.Array {
return false, false
}
for i := 0; i < dataRv.Len(); i++ {
if IsEqual(dataRv.Index(i).Interface(), elem) {
return true, true
}
}
return true, false
}
// StringsContains check string slice contains sub-string
func StringsContains(ss []string, sub string) bool {
for _, v := range ss {
if v == sub {
return true
}
}
return false
}
var (
// check is number: int or float
numReg = regexp.MustCompile(`^[-+]?\d*\.?\d+$`)
// is positive number: int or float
pNumReg = regexp.MustCompile(`^\d*\.?\d+$`)
)
// IsNumeric returns true if the given string is a numeric, otherwise false.
func IsNumeric(s string) bool {
if s == "" {
return false
}
return numReg.MatchString(s)
}
// IsPositiveNum check input string is positive number
func IsPositiveNum(s string) bool {
if s == "" {
return false
}
if s[0] == '-' {
return false
}
return pNumReg.MatchString(s)
}
// IsHttpURL check input is http/https url
func IsHttpURL(s string) bool {
return strings.HasPrefix(s, "http://") || strings.HasPrefix(s, "https://")
}
// IndexByteAfter find index of byte after startIndex. return -1 if not found
//
// eg:
//
// IndexByteAfter("abcabc", 'b', 0) = 1
// IndexByteAfter("abcabc", 'b', 2) = 4
func IndexByteAfter(s string, b byte, startIndex int) int {
idx := strings.IndexByte(s[startIndex:], b)
if idx < 0 {
return -1
}
return idx + startIndex
}
+40
View File
@@ -0,0 +1,40 @@
package checkfn
import (
"os"
"strings"
)
var detectedWSL bool
var detectedWSLContents string
// IsWSL system env
// https://github.com/Microsoft/WSL/issues/423#issuecomment-221627364
func IsWSL() bool {
// ENV:
// WSL_DISTRO_NAME=Debian
if len(os.Getenv("WSL_DISTRO_NAME")) > 0 {
return true
}
if !detectedWSL {
b := make([]byte, 1024)
// `cat /proc/version`
// on mac:
// !not the file!
// on linux(debian,ubuntu,alpine):
// Linux version 4.19.121-linuxkit (root@18b3f92ade35) (gcc version 9.2.0 (Alpine 9.2.0)) #1 SMP Thu Jan 21 15:36:34 UTC 2021
// on win git bash, conEmu:
// MINGW64_NT-10.0-19042 version 3.1.7-340.x86_64 (@WIN-N0G619FD3UK) (gcc version 9.3.0 (GCC) ) 2020-10-23 13:08 UTC
// on WSL:
// Linux version 4.4.0-19041-Microsoft (Microsoft@Microsoft.com) (gcc version 5.4.0 (GCC) ) #488-Microsoft Mon Sep 01 13:43:00 PST 2020
f, err := os.Open("/proc/version")
if err == nil {
_, _ = f.Read(b) // ignore error
f.Close()
detectedWSLContents = string(b)
}
detectedWSL = true
}
return strings.Contains(detectedWSLContents, "Microsoft")
}