github.com/guyezi/gofrontend@v0.0.0-20200228202240-7a62a49e62c0/libgo/go/internal/bytealg/bytealg.c (about)

     1  /* bytealg.c -- gccgo implementations of bytealg functions
     2  
     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.  */
     6  
     7  #include <stddef.h>
     8  #include <string.h>
     9  
    10  #include "runtime.h"
    11  #include "array.h"
    12  
    13  #ifndef HAVE_MEMMEM
    14  
    15  #define memmem goMemmem
    16  
    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;
    21  
    22  	if (sl == 0) {
    23  		return in;
    24  	}
    25  	if (inl < sl) {
    26  		return nil;
    27  	}
    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);
    33  		}
    34  	}
    35  	return nil;
    36  }
    37  
    38  #endif
    39  
    40  intgo Compare(struct __go_open_array, struct __go_open_array)
    41    __asm__(GOSYM_PREFIX "internal..z2fbytealg.Compare")
    42    __attribute__((no_split_stack));
    43  
    44  intgo Compare(struct __go_open_array a, struct __go_open_array b)
    45  {
    46  	intgo l;
    47  
    48  	l = a.__count;
    49  	if (b.__count < l) {
    50  		l = b.__count;
    51  	}
    52  	if (l > 0 && a.__values != b.__values) {
    53  		int i;
    54  
    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;
    60  		}
    61  	}
    62  	if (a.__count < b.__count) {
    63  		return -1;
    64  	}
    65  	if (a.__count > b.__count) {
    66  		return +1;
    67  	}
    68  	return 0;
    69  }
    70  
    71  intgo IndexByte(struct __go_open_array, byte)
    72    __asm__(GOSYM_PREFIX "internal..z2fbytealg.IndexByte")
    73    __attribute__((no_split_stack));
    74  
    75  intgo IndexByte(struct __go_open_array b, byte c)
    76  {
    77  	const byte *p;
    78  
    79  	p = __builtin_memchr(b.__values, c, (size_t)(b.__count));
    80  	if (p == nil) {
    81  		return -1;
    82  	}
    83  	return p - (byte *)(b.__values);
    84  }
    85  
    86  
    87  intgo IndexByteString(String, byte)
    88    __asm__(GOSYM_PREFIX "internal..z2fbytealg.IndexByteString")
    89    __attribute__((no_split_stack));
    90  
    91  intgo IndexByteString(String s, byte c)
    92  {
    93  	const byte *p;
    94  
    95  	p = __builtin_memchr(s.str, c, (size_t)(s.len));
    96  	if (p == nil) {
    97  		return -1;
    98  	}
    99  	return p - s.str;
   100  }
   101  
   102  intgo Index(struct __go_open_array, struct __go_open_array)
   103    __asm__(GOSYM_PREFIX "internal..z2fbytealg.Index")
   104    __attribute__((no_split_stack));
   105  
   106  intgo Index(struct __go_open_array a, struct __go_open_array b)
   107  {
   108  	const byte* p;
   109  
   110  	p = memmem(a.__values, a.__count, b.__values, b.__count);
   111  	if (p == nil) {
   112  		return -1;
   113  	}
   114  	return p - (const byte*)(a.__values);
   115  }
   116  
   117  intgo IndexString(String, String)
   118    __asm__(GOSYM_PREFIX "internal..z2fbytealg.IndexString")
   119    __attribute__((no_split_stack));
   120  
   121  intgo IndexString(String a, String b)
   122  {
   123  	const byte* p;
   124  
   125  	p = memmem(a.str, a.len, b.str, b.len);
   126  	if (p == nil) {
   127  		return -1;
   128  	}
   129  	return p - a.str;
   130  }