mirror of
https://github.com/google/pebble.git
synced 2025-05-23 19:54:53 +00:00
Import of the watch repository from Pebble
This commit is contained in:
commit
3b92768480
10334 changed files with 2564465 additions and 0 deletions
37
platform/tintin/boot/libc/string/atoi.c
Normal file
37
platform/tintin/boot/libc/string/atoi.c
Normal file
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* Copyright 2024 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
///////////////////////////////////////
|
||||
// Implements:
|
||||
// int atoi(const char *nptr);
|
||||
// long int atol(const char *nptr);
|
||||
///////////////////////////////////////
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <limits.h>
|
||||
|
||||
intmax_t strtoX_core(const char * restrict nptr, char ** restrict endptr, int base, bool do_errors,
|
||||
intmax_t max, intmax_t min);
|
||||
|
||||
int atoi(const char *nptr) {
|
||||
return strtoX_core(nptr, NULL, 10, false, INT_MAX, INT_MIN);
|
||||
}
|
||||
|
||||
long int atol(const char *nptr) {
|
||||
return strtoX_core(nptr, NULL, 10, false, INT_MAX, INT_MIN);
|
||||
}
|
32
platform/tintin/boot/libc/string/memchr.c
Normal file
32
platform/tintin/boot/libc/string/memchr.c
Normal file
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* Copyright 2024 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
///////////////////////////////////////
|
||||
// Implements:
|
||||
// void *memchr(const void *s, int c, size_t n);
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
void *memchr(const void *s, int c, size_t n) {
|
||||
const unsigned char *p = (const unsigned char*)s;
|
||||
unsigned char ch = (unsigned char)c;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
if (p[i] == ch) {
|
||||
return (void*)&p[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
35
platform/tintin/boot/libc/string/memcmp.c
Normal file
35
platform/tintin/boot/libc/string/memcmp.c
Normal file
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Copyright 2024 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
///////////////////////////////////////
|
||||
// Implements:
|
||||
// int memcmp(const void *s1, const void *s2, size_t n);
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
int memcmp(const void *s1, const void *s2, size_t n) {
|
||||
const unsigned char *p1 = (const unsigned char*)s1;
|
||||
const unsigned char *p2 = (const unsigned char*)s2;
|
||||
while (n--) {
|
||||
int diff = *p1 - *p2;
|
||||
if (diff) {
|
||||
return diff;
|
||||
}
|
||||
p1++;
|
||||
p2++;
|
||||
}
|
||||
return 0;
|
||||
}
|
48
platform/tintin/boot/libc/string/memcpy.c
Normal file
48
platform/tintin/boot/libc/string/memcpy.c
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* Copyright 2024 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
///////////////////////////////////////
|
||||
// Implements:
|
||||
// void *memcpy(void *s1, const void *s2, size_t n);
|
||||
// void *memmove(void *s1, const void *s2, size_t n);
|
||||
///////////////////////////////////////
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
void *memcpy(void * restrict s1, const void * restrict s2, size_t n) {
|
||||
char *dest = (char*)s1;
|
||||
const char *src = (const char*)s2;
|
||||
while (n--) {
|
||||
*dest++ = *src++;
|
||||
}
|
||||
return s1;
|
||||
}
|
||||
|
||||
void *memmove(void * restrict s1, const void * restrict s2, size_t n) {
|
||||
char *dest = (char*)s1;
|
||||
const char *src = (const char*)s2;
|
||||
if (dest <= src) {
|
||||
while (n--) {
|
||||
*dest++ = *src++;
|
||||
}
|
||||
} else {
|
||||
while (n--) {
|
||||
dest[n] = src[n];
|
||||
}
|
||||
}
|
||||
return s1;
|
||||
}
|
30
platform/tintin/boot/libc/string/memset.c
Normal file
30
platform/tintin/boot/libc/string/memset.c
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* Copyright 2024 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
///////////////////////////////////////
|
||||
// Implements:
|
||||
// void *memset(void *s, int c, size_t n);
|
||||
///////////////////////////////////////
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
void *memset(void *s, int c, size_t n) {
|
||||
unsigned char *p = (unsigned char*)s;
|
||||
while (n--) {
|
||||
*p++ = (unsigned char)c;
|
||||
}
|
||||
return s;
|
||||
}
|
45
platform/tintin/boot/libc/string/strcat.c
Normal file
45
platform/tintin/boot/libc/string/strcat.c
Normal file
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* Copyright 2024 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
///////////////////////////////////////
|
||||
// Implements:
|
||||
// char *strcat(char *s1, const char *s2);
|
||||
// char *strncat(char *s1, const char *s2, size_t n);
|
||||
///////////////////////////////////////
|
||||
// Notes:
|
||||
// Tuned for code size.
|
||||
|
||||
#include <string.h>
|
||||
|
||||
char *strcat(char * restrict s1, const char * restrict s2) {
|
||||
char *rc = s1;
|
||||
s1 += strlen(s1);
|
||||
strcpy(s1, s2);
|
||||
return rc;
|
||||
}
|
||||
|
||||
char *strncat(char * restrict s1, const char * restrict s2, size_t n) {
|
||||
char * rc = s1;
|
||||
s1 += strlen(s1);
|
||||
|
||||
size_t strn = strlen(s2);
|
||||
if (strn > n) {
|
||||
strn = n;
|
||||
}
|
||||
memcpy(s1, s2, strn);
|
||||
s1[strn] = '\0';
|
||||
return rc;
|
||||
}
|
40
platform/tintin/boot/libc/string/strchr.c
Normal file
40
platform/tintin/boot/libc/string/strchr.c
Normal file
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* Copyright 2024 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
///////////////////////////////////////
|
||||
// Implements:
|
||||
// char *strchr(const char *s1, int c);
|
||||
// char *strrchr(const char *s1, int c);
|
||||
///////////////////////////////////////
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
char *strchr(const char *s, int c) {
|
||||
size_t n = strlen(s) + 1;
|
||||
return memchr(s, c, n);
|
||||
}
|
||||
|
||||
char *strrchr(const char *s, int c) {
|
||||
char ch = (char)c;
|
||||
size_t i = strlen(s);
|
||||
do {
|
||||
if (s[i] == ch) {
|
||||
return (char*)&s[i];
|
||||
}
|
||||
} while (i--);
|
||||
return NULL;
|
||||
}
|
39
platform/tintin/boot/libc/string/strcmp.c
Normal file
39
platform/tintin/boot/libc/string/strcmp.c
Normal file
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* Copyright 2024 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
///////////////////////////////////////
|
||||
// Implements:
|
||||
// int strcmp(const char *s1, const char *s2);
|
||||
// int strncmp(const char *s1, const char *s2, size_t n);
|
||||
///////////////////////////////////////
|
||||
// Notes:
|
||||
// Tuned for code size.
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
int strcmp(const char *s1, const char *s2) {
|
||||
size_t n = strlen(s1) + 1;
|
||||
return memcmp(s1, s2, n);
|
||||
}
|
||||
|
||||
int strncmp(const char *s1, const char *s2, size_t n) {
|
||||
size_t strn = strlen(s1) + 1;
|
||||
if (strn < n) {
|
||||
n = strn;
|
||||
}
|
||||
return memcmp(s1, s2, n);
|
||||
}
|
44
platform/tintin/boot/libc/string/strcpy.c
Normal file
44
platform/tintin/boot/libc/string/strcpy.c
Normal file
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright 2024 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
///////////////////////////////////////
|
||||
// Implements:
|
||||
// char *strcpy(char *s1, const char *s2);
|
||||
// char *strncpy(char *s1, const char *s2, size_t n);
|
||||
///////////////////////////////////////
|
||||
// Notes:
|
||||
// Tuned for code size.
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
char *strcpy(char * restrict s1, const char * restrict s2) {
|
||||
size_t n = strlen(s2) + 1;
|
||||
return memcpy(s1, s2, n);
|
||||
}
|
||||
|
||||
char *strncpy(char * restrict s1, const char * restrict s2, size_t n) {
|
||||
char *rc = s1;
|
||||
size_t strn = strlen(s2) + 1;
|
||||
if (strn > n) {
|
||||
strn = n;
|
||||
}
|
||||
memcpy(s1, s2, strn);
|
||||
if (n > strn) {
|
||||
memset(s1 + strn, '\0', n - strn);
|
||||
}
|
||||
return rc;
|
||||
}
|
39
platform/tintin/boot/libc/string/strlen.c
Normal file
39
platform/tintin/boot/libc/string/strlen.c
Normal file
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* Copyright 2024 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
///////////////////////////////////////
|
||||
// Implements:
|
||||
// size_t strlen(const char *s);
|
||||
// size_t strnlen(const char *s, size_t maxlen);
|
||||
///////////////////////////////////////
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
size_t strlen(const char *s) {
|
||||
size_t len = 0;
|
||||
while (*s++) {
|
||||
len++;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
size_t strnlen(const char *s, size_t maxlen) {
|
||||
size_t len = 0;
|
||||
while (*s++ && maxlen--) {
|
||||
len++;
|
||||
}
|
||||
return len;
|
||||
}
|
57
platform/tintin/boot/libc/string/strspn.c
Normal file
57
platform/tintin/boot/libc/string/strspn.c
Normal file
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* Copyright 2024 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
///////////////////////////////////////
|
||||
// Implements:
|
||||
// size_t strcspn(const char *s1, const char *s2);
|
||||
// size_t strspn(const char *s1, const char *s2);
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
size_t strcspn(const char *s1, const char *s2) {
|
||||
size_t len = 0;
|
||||
const char *p;
|
||||
while (s1[len]) {
|
||||
p = s2;
|
||||
while (*p) {
|
||||
if (s1[len] == *p++) {
|
||||
return len;
|
||||
}
|
||||
}
|
||||
len++;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
size_t strspn(const char *s1, const char *s2) {
|
||||
size_t len = 0;
|
||||
const char *p;
|
||||
while (s1[len]) {
|
||||
p = s2;
|
||||
while (*p) {
|
||||
if (s1[len] == *p) {
|
||||
break;
|
||||
}
|
||||
p++;
|
||||
}
|
||||
if (!*p) {
|
||||
return len;
|
||||
}
|
||||
len++;
|
||||
}
|
||||
return len;
|
||||
}
|
36
platform/tintin/boot/libc/string/strstr.c
Normal file
36
platform/tintin/boot/libc/string/strstr.c
Normal file
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* Copyright 2024 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
///////////////////////////////////////
|
||||
// Implements:
|
||||
// char *strstr(const char *s1, const char *s2);
|
||||
///////////////////////////////////////
|
||||
// Notes:
|
||||
// Tuned for code size.
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
char *strstr(const char *s1, const char *s2) {
|
||||
size_t len = strlen(s2);
|
||||
while (*s1) {
|
||||
if (strncmp(s1, s2, len) == 0) {
|
||||
return (char*)s1;
|
||||
}
|
||||
s1++;
|
||||
}
|
||||
return NULL;
|
||||
}
|
81
platform/tintin/boot/libc/string/strtol.c
Normal file
81
platform/tintin/boot/libc/string/strtol.c
Normal file
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
* Copyright 2024 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
///////////////////////////////////////
|
||||
// Implements:
|
||||
// long int strtol(const char *nptr, char **endptr, int base);
|
||||
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <limits.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
intmax_t strtoX_core(const char * restrict nptr, char ** restrict endptr, int base, bool do_errors,
|
||||
intmax_t max, intmax_t min) {
|
||||
intmax_t value = 0;
|
||||
int sign = 1;
|
||||
while (isspace((int)*nptr)) {
|
||||
nptr++;
|
||||
}
|
||||
switch (*nptr) {
|
||||
case '+': sign = 1; nptr++; break;
|
||||
case '-': sign = -1; nptr++; break;
|
||||
}
|
||||
if (nptr[0] == '0' && nptr[1] == 'x' && (base == 0 || base == 16)) {
|
||||
base = 16;
|
||||
nptr += 2;
|
||||
} else if (nptr[0] == '0' && (base == 0 || base == 8)) {
|
||||
base = 8;
|
||||
nptr++;
|
||||
} else if (base == 0) {
|
||||
base = 10;
|
||||
}
|
||||
// We break on '\0' anyways
|
||||
for (;;) {
|
||||
char ch = *nptr;
|
||||
if (ch >= '0' && ch <= '9') {
|
||||
ch = ch - '0';
|
||||
} else if (ch >= 'A' && ch <= 'Z') {
|
||||
ch = ch - 'A' + 10;
|
||||
} else if (ch >= 'a' && ch <= 'z') {
|
||||
ch = ch - 'a' + 10;
|
||||
} else { // This will catch '\0'
|
||||
break;
|
||||
}
|
||||
if (ch >= base) {
|
||||
break;
|
||||
}
|
||||
value *= base;
|
||||
value += ch;
|
||||
if (do_errors) {
|
||||
if ((sign > 0) && (value > max)) {
|
||||
value = max;
|
||||
} else if ((sign < 0) && (-value < min)) {
|
||||
value = -min;
|
||||
}
|
||||
}
|
||||
nptr++;
|
||||
}
|
||||
if (endptr) {
|
||||
*endptr = (char*)nptr;
|
||||
}
|
||||
return value * sign;
|
||||
}
|
||||
|
||||
long int strtol(const char * restrict nptr, char ** restrict endptr, int base) {
|
||||
return strtoX_core(nptr, endptr, base, true, INT_MAX, INT_MIN);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue