xref: /aosp_15_r20/external/skia/tools/unicode_comparison/go/helpers/helpers.go (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1// Copyright 2023 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4package helpers
5
6import (
7	"os"
8	"path/filepath"
9	"strconv"
10	"strings"
11)
12
13func Check(e error) {
14	if e != nil {
15		panic(e)
16	}
17}
18
19func Abs(x int) int {
20	if x < 0 {
21		return -x
22	}
23	return x
24}
25
26func SplitAsInts(str string, sep string) []int {
27	arr := strings.Split(str, sep)
28	var result []int
29	for _, i := range arr {
30		j, err := strconv.Atoi(i)
31		if err != nil {
32			return nil
33		}
34		result = append(result, j)
35	}
36	return result
37}
38
39func ExpandPath(path string) string {
40	if strings.HasPrefix(path, "~/") {
41		home, err := os.UserHomeDir()
42		Check(err)
43		return filepath.Join(home, (path)[2:])
44	}
45	return path
46}
47
48func WriteTextFile(fullFileName string, text string) {
49	err := os.WriteFile(fullFileName, []byte(text), 0666)
50	Check(err)
51}
52