Debian Codegraph @

tools/unicode_comparison/go/bridge/bridge.go

raw
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
5 package bridge
6
7 // #include <stdio.h>
8 // #include <stdlib.h>
9 // #include "../../cpp/bridge.h"
10 // #cgo LDFLAGS: -L../../../../out/Release/ -lbridge
11 import "C"
12
13 import (
14 "unsafe"
15 )
16
17 type CodeUnitFlags int
18
19 const (
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
35 type SkString struct {
36 ptr unsafe.Pointer
37 }
38
39 type IntPtr struct {
40 ptr unsafe.Pointer
41 }
42
43 func 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
49 func GetFlags(index int) int {
50 return int(C.getFlags(C.int(index)))
51 }
52
53 func 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
63 func 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
72 func 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
95 func 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
103 func 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
110 func CleanupUnicode() {
111 C.cleanup_unicode_impl()
112 }