Compare commits
No commits in common. "master" and "v0.1.1" have entirely different histories.
53
FileUtils.go
53
FileUtils.go
@ -1,53 +0,0 @@
|
|||||||
package golanghelper
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
type FileUtils struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *FileUtils) CopyFile(srcFilePath, destFilePath string, isOverwrite bool) (err error) {
|
|
||||||
var srcFileInfo os.FileInfo
|
|
||||||
srcFileInfo, err = os.Stat(srcFilePath)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if !srcFileInfo.Mode().IsRegular() {
|
|
||||||
return errors.New(fmt.Sprintf("%s is not a regular file", srcFileInfo.Name()))
|
|
||||||
}
|
|
||||||
|
|
||||||
var isNotExist bool
|
|
||||||
_, err = os.Stat(destFilePath)
|
|
||||||
if err != nil {
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
isNotExist = true
|
|
||||||
} else {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var destFile *os.File
|
|
||||||
if isNotExist == false && isOverwrite == false {
|
|
||||||
return errors.New("File already exists")
|
|
||||||
}
|
|
||||||
|
|
||||||
destFile, err = os.Create(destFilePath)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer destFile.Close()
|
|
||||||
|
|
||||||
sourceFile, err := os.Open(srcFilePath)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer sourceFile.Close()
|
|
||||||
|
|
||||||
_, err = io.Copy(destFile, sourceFile)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
package golanghelper
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
)
|
|
||||||
|
|
||||||
type JSONObject struct {
|
|
||||||
Data []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewJSONObject(object interface{}) (jsonObject JSONObject, err error) {
|
|
||||||
var data []byte
|
|
||||||
data, err = json.Marshal(object)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
jsonObject = JSONObject{Data: data}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (j *JSONObject) ToString() string {
|
|
||||||
return string(j.Data)
|
|
||||||
}
|
|
@ -33,36 +33,3 @@ func (s *StringUtils) LeftPad(str string, size int, padStr string) string {
|
|||||||
|
|
||||||
return strings.Repeat(padStr, padCount) + str
|
return strings.Repeat(padStr, padCount) + str
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StringUtils) Substring(str string, start, end int) string {
|
|
||||||
if str == "" {
|
|
||||||
return str
|
|
||||||
}
|
|
||||||
|
|
||||||
runes := []rune(str)
|
|
||||||
|
|
||||||
if end < 0 {
|
|
||||||
end = len(runes) + end
|
|
||||||
}
|
|
||||||
if start < 0 {
|
|
||||||
start = len(runes) + start
|
|
||||||
}
|
|
||||||
|
|
||||||
if end > len(str) {
|
|
||||||
end = len(str)
|
|
||||||
}
|
|
||||||
|
|
||||||
if start > end {
|
|
||||||
return EMPTY
|
|
||||||
}
|
|
||||||
|
|
||||||
if start < 0 {
|
|
||||||
start = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
if end < 0 {
|
|
||||||
end = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
return string(runes[start:end])
|
|
||||||
}
|
|
||||||
|
@ -1,51 +0,0 @@
|
|||||||
package golanghelper_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"gitlab-ce.niaulang.com/niau-lang/golanghelper"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestSubstring(t *testing.T) {
|
|
||||||
stringUtils := golanghelper.StringUtils{}
|
|
||||||
|
|
||||||
s := stringUtils.Substring("", 0, 0)
|
|
||||||
if s != "" {
|
|
||||||
t.Errorf("預期的值為空字串, 卻回傳 %v", s)
|
|
||||||
}
|
|
||||||
|
|
||||||
s = stringUtils.Substring("abc", 0, 2)
|
|
||||||
if s != "ab" {
|
|
||||||
t.Errorf("預期的值為 ab, 卻回傳 %v", s)
|
|
||||||
}
|
|
||||||
|
|
||||||
s = stringUtils.Substring("abc", 2, 0)
|
|
||||||
if s != "" {
|
|
||||||
t.Errorf("預期的值為空字串, 卻回傳 %v", s)
|
|
||||||
}
|
|
||||||
|
|
||||||
s = stringUtils.Substring("abc", 2, 4)
|
|
||||||
if s != "c" {
|
|
||||||
t.Errorf("預期的值為 c, 卻回傳 %v", s)
|
|
||||||
}
|
|
||||||
|
|
||||||
s = stringUtils.Substring("abc", 4, 6)
|
|
||||||
if s != "" {
|
|
||||||
t.Errorf("預期的值為空字串, 卻回傳 %v", s)
|
|
||||||
}
|
|
||||||
|
|
||||||
s = stringUtils.Substring("abc", 2, 2)
|
|
||||||
if s != "" {
|
|
||||||
t.Errorf("預期的值為 ab, 卻回傳 %v", s)
|
|
||||||
}
|
|
||||||
|
|
||||||
s = stringUtils.Substring("abc", -2, -1)
|
|
||||||
if s != "b" {
|
|
||||||
t.Errorf("預期的值為 b, 卻回傳 %v", s)
|
|
||||||
}
|
|
||||||
|
|
||||||
s = stringUtils.Substring("abc", -4, 2)
|
|
||||||
if s != "ab" {
|
|
||||||
t.Errorf("預期的值為 ab, 卻回傳 %v", s)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user