c-family: char8_t and aliasing in C vs C++ [PR111884]
[official-gcc.git] / libgo / go / internal / bytealg / bytealg.c
blob6c08340b38ff90111ebe2c78466d57ff810776fc
1 /* bytealg.c -- gccgo implementations of bytealg functions
3 Copyright 2018 The Go Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file. */
7 #include <stddef.h>
8 #include <string.h>
10 #include "runtime.h"
11 #include "array.h"
13 #ifndef HAVE_MEMMEM
15 #define memmem goMemmem
17 static const void *goMemmem(const void *in, size_t inl, const void *s, size_t sl) {
18 const char *p;
19 char first;
20 const char *stop;
22 if (sl == 0) {
23 return in;
25 if (inl < sl) {
26 return nil;
28 first = *(const char *)(s);
29 stop = (const char *)(in) + (inl - sl);
30 for (p = (const char *)(in); p <= stop; p++) {
31 if (*p == first && __builtin_memcmp(p + 1, (const char *)(s) + 1, sl - 1) == 0) {
32 return (const void *)(p);
35 return nil;
38 #endif
40 intgo Compare(struct __go_open_array, struct __go_open_array)
41 __asm__(GOSYM_PREFIX "internal_1bytealg.Compare")
42 __attribute__((no_split_stack));
44 intgo Compare(struct __go_open_array a, struct __go_open_array b)
46 intgo l;
48 l = a.__count;
49 if (b.__count < l) {
50 l = b.__count;
52 if (l > 0 && a.__values != b.__values) {
53 int i;
55 i = __builtin_memcmp(a.__values, b.__values, (size_t)(l));
56 if (i < 0) {
57 return -1;
58 } else if (i > 0) {
59 return 1;
62 if (a.__count < b.__count) {
63 return -1;
65 if (a.__count > b.__count) {
66 return +1;
68 return 0;
71 intgo IndexByte(struct __go_open_array, byte)
72 __asm__(GOSYM_PREFIX "internal_1bytealg.IndexByte")
73 __attribute__((no_split_stack));
75 intgo IndexByte(struct __go_open_array b, byte c)
77 const byte *p;
79 p = __builtin_memchr(b.__values, c, (size_t)(b.__count));
80 if (p == nil) {
81 return -1;
83 return p - (byte *)(b.__values);
87 intgo IndexByteString(String, byte)
88 __asm__(GOSYM_PREFIX "internal_1bytealg.IndexByteString")
89 __attribute__((no_split_stack));
91 intgo IndexByteString(String s, byte c)
93 const byte *p;
95 p = __builtin_memchr(s.str, c, (size_t)(s.len));
96 if (p == nil) {
97 return -1;
99 return p - s.str;
102 intgo Index(struct __go_open_array, struct __go_open_array)
103 __asm__(GOSYM_PREFIX "internal_1bytealg.Index")
104 __attribute__((no_split_stack));
106 intgo Index(struct __go_open_array a, struct __go_open_array b)
108 const byte* p;
110 p = memmem(a.__values, a.__count, b.__values, b.__count);
111 if (p == nil) {
112 return -1;
114 return p - (const byte*)(a.__values);
117 intgo IndexString(String, String)
118 __asm__(GOSYM_PREFIX "internal_1bytealg.IndexString")
119 __attribute__((no_split_stack));
121 intgo IndexString(String a, String b)
123 const byte* p;
125 p = memmem(a.str, a.len, b.str, b.len);
126 if (p == nil) {
127 return -1;
129 return p - a.str;