Skip to content

Commit af7e799

Browse files
authored
Merge pull request #1 from dolthub/daylon/stack-and-buffer
Changed calls to use persistent stack, now accepts a string buffer size
2 parents a68166c + b9d050b commit af7e799

File tree

4 files changed

+190
-70
lines changed

4 files changed

+190
-70
lines changed

format_repo.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
3+
# Copyright 2023 Dolthub, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -eo pipefail
18+
19+
paths=`find . -maxdepth 1 -mindepth 1 \( -name gen -prune -o -type d -print -o -type f -name '*.go' -print \)`
20+
21+
goimports -w -local github.com/dolthub/go-icu-regex $paths
22+
23+
bad_files=$(find $paths -name '*.go' | while read f; do
24+
if [[ $(awk '/import \(/{flag=1;next}/\)/{flag=0}flag' < $f | egrep -c '$^') -gt 2 ]]; then
25+
echo $f
26+
fi
27+
done)
28+
29+
if [ "$bad_files" != "" ]; then
30+
for f in $bad_files; do
31+
awk '/import \(/{flag=1}/\)/{flag=0}flag&&!/^$/||!flag' < "$f" > "$f.bak"
32+
mv "$f.bak" "$f"
33+
done
34+
goimports -w -local github.com/dolthub/go-icu-regex .
35+
fi

functions.go

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,18 @@ type CharPtr int32
2626

2727
// void* malloc(size_t size)
2828
func (pr *privateRegex) malloc(ctx context.Context, sz uint32) (uint32, error) {
29-
res, err := pr.f_malloc.Call(ctx, uint64(sz))
29+
pr.callStack[0] = uint64(sz)
30+
err := pr.f_malloc.CallWithStack(ctx, pr.callStack[:])
3031
if err != nil {
3132
return 0, err
3233
}
33-
return uint32(res[0]), nil
34+
return uint32(pr.callStack[0]), nil
3435
}
3536

3637
// void free(void* ptr)
3738
func (pr *privateRegex) free(ctx context.Context, ptr uint32) error {
38-
_, err := pr.f_free.Call(ctx, uint64(ptr))
39-
return err
39+
pr.callStack[0] = uint64(ptr)
40+
return pr.f_free.CallWithStack(ctx, pr.callStack[:])
4041
}
4142

4243
// UChar* replace(URegularExpression* regexp, UChar* replacement, int replacementLen, UChar* original, int originalSize, int start, int occurrence, int* returnSize)
@@ -54,11 +55,12 @@ func (pr *privateRegex) replace(ctx context.Context, regex URegularExpressionPtr
5455
*returnSize = int(res)
5556
}()
5657

57-
res, err := pr.f_replace.Call(ctx, uint64(regex), uint64(replacement), uint64(replacementLen), uint64(original), uint64(originalSize), uint64(start), uint64(occurrence), returnSizeAddr)
58+
copy(pr.callStack[:], []uint64{uint64(regex), uint64(replacement), uint64(replacementLen), uint64(original), uint64(originalSize), uint64(start), uint64(occurrence), returnSizeAddr})
59+
err = pr.f_replace.CallWithStack(ctx, pr.callStack[:])
5860
if err != nil {
5961
return 0, err
6062
}
61-
return UCharPtr(res[0]), err
63+
return UCharPtr(pr.callStack[0]), err
6264
}
6365

6466
// URegularExpression* uregex_open(const UChar* pattern, int32_t patternLength, uint32_t flags, UErrorCode* status);
@@ -76,17 +78,18 @@ func (pr *privateRegex) uregex_open(ctx context.Context, str UCharPtr, strlen in
7678
*uerr = UErrorCode(res)
7779
}()
7880

79-
res, err := pr.f_uregex_open.Call(ctx, uint64(str), uint64(strlen), uint64(flags), uint64(0), uerrAddr)
81+
copy(pr.callStack[:], []uint64{uint64(str), uint64(strlen), uint64(flags), uint64(0), uerrAddr})
82+
err = pr.f_uregex_open.CallWithStack(ctx, pr.callStack[:])
8083
if err != nil {
8184
return 0, err
8285
}
83-
return URegularExpressionPtr(res[0]), nil
86+
return URegularExpressionPtr(pr.callStack[0]), nil
8487
}
8588

8689
// void uregex_close(URegularExpression* regexp)
8790
func (pr *privateRegex) uregex_close(ctx context.Context, p URegularExpressionPtr) error {
88-
_, err := pr.f_uregex_close.Call(ctx, uint64(p))
89-
return err
91+
pr.callStack[0] = uint64(p)
92+
return pr.f_uregex_close.CallWithStack(ctx, pr.callStack[:])
9093
}
9194

9295
// int32_t uregex_start(URegularExpression *regexp, int32_t groupNum, UErrorCode* status)
@@ -104,11 +107,12 @@ func (pr *privateRegex) uregex_start(ctx context.Context, regex URegularExpressi
104107
*uerr = UErrorCode(res)
105108
}()
106109

107-
res, err := pr.f_uregex_start.Call(ctx, uint64(regex), uint64(group), uerrAddr)
110+
copy(pr.callStack[:], []uint64{uint64(regex), uint64(group), uerrAddr})
111+
err = pr.f_uregex_start.CallWithStack(ctx, pr.callStack[:])
108112
if err != nil {
109113
return 0, err
110114
}
111-
return int32(res[0]), nil
115+
return int32(pr.callStack[0]), nil
112116
}
113117

114118
// int32_t uregex_end(URegularExpression* regexp, int32_t groupNum, UErrorCode* status)
@@ -126,11 +130,12 @@ func (pr *privateRegex) uregex_end(ctx context.Context, regex URegularExpression
126130
*uerr = UErrorCode(res)
127131
}()
128132

129-
res, err := pr.f_uregex_end.Call(ctx, uint64(regex), uint64(group), uerrAddr)
133+
copy(pr.callStack[:], []uint64{uint64(regex), uint64(group), uerrAddr})
134+
err = pr.f_uregex_end.CallWithStack(ctx, pr.callStack[:])
130135
if err != nil {
131136
return 0, err
132137
}
133-
return int32(res[0]), nil
138+
return int32(pr.callStack[0]), nil
134139
}
135140

136141
// UBool uregex_find(URegularExpression* regexp, int32_t startIndex, UErrorCode* status)
@@ -148,11 +153,12 @@ func (pr *privateRegex) uregex_find(ctx context.Context, regex URegularExpressio
148153
*uerr = UErrorCode(res)
149154
}()
150155

151-
res, err := pr.f_uregex_find.Call(ctx, uint64(regex), uint64(startIndex), uerrAddr)
156+
copy(pr.callStack[:], []uint64{uint64(regex), uint64(startIndex), uerrAddr})
157+
err = pr.f_uregex_find.CallWithStack(ctx, pr.callStack[:])
152158
if err != nil {
153159
return false, err
154160
}
155-
return res[0] != 0, nil
161+
return pr.callStack[0] != 0, nil
156162
}
157163

158164
// UBool uregex_findNext(URegularExpression* regexp, UErrorCode* status)
@@ -170,11 +176,12 @@ func (pr *privateRegex) uregex_findNext(ctx context.Context, regex URegularExpre
170176
*uerr = UErrorCode(res)
171177
}()
172178

173-
res, err := pr.f_uregex_findNext.Call(ctx, uint64(regex), uerrAddr)
179+
copy(pr.callStack[:], []uint64{uint64(regex), uerrAddr})
180+
err = pr.f_uregex_findNext.CallWithStack(ctx, pr.callStack[:])
174181
if err != nil {
175182
return false, err
176183
}
177-
return res[0] != 0, nil
184+
return pr.callStack[0] != 0, nil
178185
}
179186

180187
// UChar* uregex_getText(URegularExpression *regexp, int32_t* textLength, UErrorCode* status)
@@ -203,11 +210,12 @@ func (pr *privateRegex) uregex_getText(ctx context.Context, p URegularExpression
203210
*textLength = int(res)
204211
}()
205212

206-
res, err := pr.f_uregex_getText.Call(ctx, uint64(p), textLengthAddr, uerrAddr)
213+
copy(pr.callStack[:], []uint64{uint64(p), textLengthAddr, uerrAddr})
214+
err = pr.f_uregex_getText.CallWithStack(ctx, pr.callStack[:])
207215
if err != nil {
208216
return 0, err
209217
}
210-
return UCharPtr(res[0]), nil
218+
return UCharPtr(pr.callStack[0]), nil
211219
}
212220

213221
// void uregex_setText(URegularExpression* regexp, const UChar* text, int32_t textLength, UErrorCode* status)
@@ -225,8 +233,8 @@ func (pr *privateRegex) uregex_setText(ctx context.Context, p URegularExpression
225233
*uerr = UErrorCode(res)
226234
}()
227235

228-
_, err = pr.f_uregex_setText.Call(ctx, uint64(p), uint64(str), uint64(strlen), uerrAddr)
229-
return err
236+
copy(pr.callStack[:], []uint64{uint64(p), uint64(str), uint64(strlen), uerrAddr})
237+
return pr.f_uregex_setText.CallWithStack(ctx, pr.callStack[:])
230238
}
231239

232240
// int32_t uregex_replaceFirst(URegularExpression* regexp, const UChar* replacementText, int32_t replacementLength, UChar* destBuf, int32_t destCapacity, UErrorCode* status);
@@ -245,11 +253,12 @@ func (pr *privateRegex) uregex_replaceFirst(ctx context.Context, p URegularExpre
245253
*uerr = UErrorCode(res)
246254
}()
247255

248-
res, err := pr.f_uregex_replaceFirst.Call(ctx, uint64(p), uint64(replacementText), uint64(replacementLength), uint64(destBuf), uint64(destCapacity), uerrAddr)
256+
copy(pr.callStack[:], []uint64{uint64(p), uint64(replacementText), uint64(replacementLength), uint64(destBuf), uint64(destCapacity), uerrAddr})
257+
err = pr.f_uregex_replaceFirst.CallWithStack(ctx, pr.callStack[:])
249258
if err != nil {
250259
return 0, err
251260
}
252-
return int(res[0]), nil
261+
return int(pr.callStack[0]), nil
253262
}
254263

255264
// int32_t uregex_replaceAll(URegularExpression* regexp, const UChar* replacementText, int32_t replacementLength, UChar* destBuf, int32_t destCapacity, UErrorCode* status);
@@ -268,11 +277,12 @@ func (pr *privateRegex) uregex_replaceAll(ctx context.Context, p URegularExpress
268277
*uerr = UErrorCode(res)
269278
}()
270279

271-
res, err := pr.f_uregex_replaceAll.Call(ctx, uint64(p), uint64(replacementText), uint64(replacementLength), uint64(destBuf), uint64(destCapacity), uerrAddr)
280+
copy(pr.callStack[:], []uint64{uint64(p), uint64(replacementText), uint64(replacementLength), uint64(destBuf), uint64(destCapacity), uerrAddr})
281+
err = pr.f_uregex_replaceAll.CallWithStack(ctx, pr.callStack[:])
272282
if err != nil {
273283
return 0, err
274284
}
275-
return int(res[0]), nil
285+
return int(pr.callStack[0]), nil
276286
}
277287

278288
// int32_t uregex_appendReplacement(URegularExpression* regexp, UChar* replacementText, int32_t replacementLength, UChar** destBuf, int32_t* destCapacity, UErrorCode* status)
@@ -311,11 +321,12 @@ func (pr *privateRegex) uregex_appendReplacement(ctx context.Context, p URegular
311321
*destCapacity = int(res)
312322
}()
313323

314-
res, err := pr.f_uregex_appendReplacement.Call(ctx, uint64(p), uint64(replacementText), uint64(replacementLength), destBufAddr, destCapacityAddr, uerrAddr)
324+
copy(pr.callStack[:], []uint64{uint64(p), uint64(replacementText), uint64(replacementLength), destBufAddr, destCapacityAddr, uerrAddr})
325+
err = pr.f_uregex_appendReplacement.CallWithStack(ctx, pr.callStack[:])
315326
if err != nil {
316327
return 0, err
317328
}
318-
return int(res[0]), nil
329+
return int(pr.callStack[0]), nil
319330
}
320331

321332
// int32_t uregex_appendTail(URegularExpression* regexp, UChar** destBuf, int32_t* destCapacity, UErrorCode* status)
@@ -354,8 +365,8 @@ func (pr *privateRegex) uregex_appendTail(ctx context.Context, p URegularExpress
354365
*destCapacity = int(res)
355366
}()
356367

357-
_, err = pr.f_uregex_appendTail.Call(ctx, uint64(p), destBufAddr, destCapacityAddr, uerrAddr)
358-
return err
368+
copy(pr.callStack[:], []uint64{uint64(p), destBufAddr, destCapacityAddr, uerrAddr})
369+
return pr.f_uregex_appendTail.CallWithStack(ctx, pr.callStack[:])
359370
}
360371

361372
// char* u_strToUTF8(char* dest, int32_t destCapacity, int32_t* pDestLength, const UChar* src, int32_t srcLength, UErrorCode* pErrorCode)
@@ -385,8 +396,9 @@ func (pr *privateRegex) u_strToUTF8(ctx context.Context, buff CharPtr, bufflen i
385396
*outlen = int(res)
386397
}()
387398
}
388-
_, err = pr.f_u_strToUTF8.Call(ctx, uint64(buff), uint64(bufflen), uint64(outlenptr), uint64(str), uint64(strlen), uerrAddr)
389-
return err
399+
400+
copy(pr.callStack[:], []uint64{uint64(buff), uint64(bufflen), uint64(outlenptr), uint64(str), uint64(strlen), uerrAddr})
401+
return pr.f_u_strToUTF8.CallWithStack(ctx, pr.callStack[:])
390402
}
391403

392404
// UChar* u_strFromUTF8(UChar* dest, int32_t destCapacity, int32_t* pDestLength, const char* src, int32_t srcLength, UErrorCode* pErrorCode)
@@ -417,6 +429,7 @@ func (pr *privateRegex) u_strFromUTF8(ctx context.Context, buff UCharPtr, buffle
417429
*outlen = int(res)
418430
}()
419431
}
420-
_, err = pr.f_u_strFromUTF8.Call(ctx, uint64(buff), uint64(bufflen), uint64(outlenptr), uint64(str), uint64(strlen), uerrAddr)
421-
return err
432+
433+
copy(pr.callStack[:], []uint64{uint64(buff), uint64(bufflen), uint64(outlenptr), uint64(str), uint64(strlen), uerrAddr})
434+
return pr.f_u_strFromUTF8.CallWithStack(ctx, pr.callStack[:])
422435
}

0 commit comments

Comments
 (0)