xref: /aosp_15_r20/external/skia/tools/unicode_comparison/go/bridge/bridge.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.
4
5package bridge
6
7// #include <stdio.h>
8// #include <stdlib.h>
9// #include "../../cpp/bridge.h"
10// #cgo LDFLAGS: -L../../../../out/Release/ -lbridge
11import "C"
12
13import (
14	"unsafe"
15)
16
17type CodeUnitFlags int
18
19const (
20	kNoCodeUnitFlag        CodeUnitFlags = 0x00
21	kPartOfWhiteSpaceBreak               = 0x01
22	kGraphemeStart                       = 0x02
23	kSoftLineBreakBefore                 = 0x04
24	kHardLineBreakBefore                 = 0x08
25	kPartOfIntraWordBreak                = 0x10
26	kControl                             = 0x20
27	kTabulation                          = 0x40
28	kGlyphClusterStart                   = 0x80
29	kIdeographic                         = 0x100
30	kEmoji                               = 0x200
31	kWordBreak                           = 0x400
32	kSentenceBreak                       = 0x800
33)
34
35type SkString struct {
36	ptr unsafe.Pointer
37}
38
39type IntPtr struct {
40	ptr unsafe.Pointer
41}
42
43func PerfComputeCodeunitFlags(text string) float64 {
44	cs := C.CString(text)
45	defer C.free(unsafe.Pointer(cs))
46	return float64(C.perf_compute_codeunit_flags(cs))
47}
48
49func GetFlags(index int) int {
50	return int(C.getFlags(C.int(index)))
51}
52
53func GetSentences(text string) []uint {
54	cs := C.CString(text)
55	defer C.free(unsafe.Pointer(cs))
56	var length C.int
57	var p = C.getSentences(cs, &length)
58	var result = make([]uint, length)
59	copy(result, unsafe.Slice((*uint)(unsafe.Pointer(p)), int(length)))
60	return result
61}
62
63func TrimSentence(text string, length *int, limit int) bool {
64	cs := C.CString(text)
65	defer C.free(unsafe.Pointer(cs))
66	var num C.int
67	result := C.trimSentence(cs, &num, C.int(limit))
68	*length = int(num)
69	return bool(result)
70}
71
72func FlagsToString(flags int) string {
73	result := ""
74	if (flags & kGraphemeStart) != 0 {
75		result += "G"
76	}
77	if (flags & kSoftLineBreakBefore) != 0 {
78		result += "S"
79	}
80	if (flags & kHardLineBreakBefore) != 0 {
81		result += "H"
82	}
83	if (flags & kPartOfWhiteSpaceBreak) != 0 {
84		result += "W"
85	}
86	if (flags & kWordBreak) != 0 {
87		result += "D"
88	}
89	if (flags & kControl) != 0 {
90		result += "C"
91	}
92	return result
93}
94
95func ToUpper(str string) SkString {
96	cs := C.CString(str)
97	defer C.free(unsafe.Pointer(cs))
98	return SkString{
99		ptr: C.toUpper(cs),
100	}
101}
102
103func InitUnicode(impl string) bool {
104	cs := C.CString(impl)
105	defer C.free(unsafe.Pointer(cs))
106	result := C.init_skunicode_impl(cs)
107	return bool(result)
108}
109
110func CleanupUnicode() {
111	C.cleanup_unicode_impl()
112}
113