2023-10-02 18:15:41 +02:00
|
|
|
/*
|
|
|
|
|
Copyright © 2023 Thomas von Dein
|
|
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package lib
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
2024-05-07 18:01:12 +02:00
|
|
|
"fmt"
|
2023-10-02 18:15:41 +02:00
|
|
|
"regexp"
|
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
|
|
"github.com/glycerine/zygomys/zygo"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Splice2SexpList(list []string) zygo.Sexp {
|
|
|
|
|
slist := []zygo.Sexp{}
|
|
|
|
|
|
|
|
|
|
for _, item := range list {
|
|
|
|
|
slist = append(slist, &zygo.SexpStr{S: item})
|
|
|
|
|
}
|
2024-05-07 18:01:12 +02:00
|
|
|
|
2023-10-02 18:15:41 +02:00
|
|
|
return zygo.MakeList(slist)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func StringReSplit(env *zygo.Zlisp, name string, args []zygo.Sexp) (zygo.Sexp, error) {
|
|
|
|
|
if len(args) < 2 {
|
2025-01-10 18:26:33 +01:00
|
|
|
return zygo.SexpNull, errors.New("expecting 2 arguments: <string>, <regex>")
|
2023-10-02 18:15:41 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-07 18:01:12 +02:00
|
|
|
var separator, input string
|
2023-10-02 18:15:41 +02:00
|
|
|
|
|
|
|
|
switch t := args[0].(type) {
|
|
|
|
|
case *zygo.SexpStr:
|
|
|
|
|
input = t.S
|
|
|
|
|
default:
|
2025-01-10 18:26:33 +01:00
|
|
|
return zygo.SexpNull, errors.New("first argument must be a string")
|
2023-10-02 18:15:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch t := args[1].(type) {
|
|
|
|
|
case *zygo.SexpStr:
|
|
|
|
|
separator = t.S
|
|
|
|
|
default:
|
2025-01-10 18:26:33 +01:00
|
|
|
return zygo.SexpNull, errors.New("second argument must be a string")
|
2023-10-02 18:15:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sep := regexp.MustCompile(separator)
|
|
|
|
|
|
|
|
|
|
return Splice2SexpList(sep.Split(input, -1)), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func String2Int(env *zygo.Zlisp, name string, args []zygo.Sexp) (zygo.Sexp, error) {
|
|
|
|
|
var number int
|
|
|
|
|
|
|
|
|
|
switch t := args[0].(type) {
|
|
|
|
|
case *zygo.SexpStr:
|
|
|
|
|
num, err := strconv.Atoi(t.S)
|
2024-05-07 18:01:12 +02:00
|
|
|
|
2023-10-02 18:15:41 +02:00
|
|
|
if err != nil {
|
2024-05-07 18:01:12 +02:00
|
|
|
return zygo.SexpNull, fmt.Errorf("failed to convert string to number: %w", err)
|
2023-10-02 18:15:41 +02:00
|
|
|
}
|
2024-05-07 18:01:12 +02:00
|
|
|
|
2023-10-02 18:15:41 +02:00
|
|
|
number = num
|
2024-05-07 18:01:12 +02:00
|
|
|
|
2023-10-02 18:15:41 +02:00
|
|
|
default:
|
2024-05-07 18:01:12 +02:00
|
|
|
return zygo.SexpNull, errors.New("argument must be a string")
|
2023-10-02 18:15:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &zygo.SexpInt{Val: int64(number)}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-10 18:26:33 +01:00
|
|
|
func RegMatch(env *zygo.Zlisp, name string, args []zygo.Sexp) (zygo.Sexp, error) {
|
|
|
|
|
if len(args) != 2 {
|
|
|
|
|
return zygo.SexpNull, fmt.Errorf("argument must be <regexp>, <string>")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
arguments := []string{}
|
|
|
|
|
|
|
|
|
|
for _, arg := range args {
|
|
|
|
|
switch t := arg.(type) {
|
|
|
|
|
case *zygo.SexpStr:
|
|
|
|
|
arguments = append(arguments, t.S)
|
|
|
|
|
default:
|
|
|
|
|
return zygo.SexpNull, errors.New("argument must be a string")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reg := regexp.MustCompile(arguments[0])
|
|
|
|
|
|
|
|
|
|
return &zygo.SexpBool{Val: reg.MatchString(arguments[1])}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 18:15:41 +02:00
|
|
|
func RegisterLib(env *zygo.Zlisp) {
|
|
|
|
|
env.AddFunction("resplit", StringReSplit)
|
|
|
|
|
env.AddFunction("atoi", String2Int)
|
2025-01-10 18:26:33 +01:00
|
|
|
env.AddFunction("matchre", RegMatch)
|
2023-10-02 18:15:41 +02:00
|
|
|
}
|