Initial import

This commit is contained in:
Justine Tunney 2020-06-15 07:18:57 -07:00
commit c91b3c5006
14915 changed files with 590219 additions and 0 deletions

24
libc/conv/abs.c Normal file
View file

@ -0,0 +1,24 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
int(abs)(int x) {
return 0 < x ? x : -x;
}

32
libc/conv/atoi.c Normal file
View file

@ -0,0 +1,32 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
#include "libc/conv/strlol.h"
#include "libc/limits.h"
/**
* Decodes decimal number from ASCII string.
*
* @param s is a non-null NUL-terminated string
* @return the decoded signed saturated number
* @note calling strtoimax() directly with base 0 permits greater
* flexibility in terms of inputs
*/
int atoi(const char *s) { return STRLOL(s, NULL, 10, INT_MIN, INT_MAX); }

24
libc/conv/atol.c Normal file
View file

@ -0,0 +1,24 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
#include "libc/conv/strlol.h"
#include "libc/limits.h"
long atol(const char *s) { return STRLOL(s, NULL, 10, LONG_MIN, LONG_MAX); }

26
libc/conv/atoll.c Normal file
View file

@ -0,0 +1,26 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
#include "libc/conv/strlol.h"
#include "libc/limits.h"
long long atoll(const char *s) {
return STRLOL(s, NULL, 10, LONG_LONG_MIN, LONG_LONG_MAX);
}

34
libc/conv/basename.c Normal file
View file

@ -0,0 +1,34 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
#include "libc/conv/isslash.h"
#include "libc/str/str.h"
/**
* Returns pointer to last filename component in path.
*
* Both / and \ are are considered valid component separators on all
* platforms. Trailing slashes are ignored. We don't grant special
* consideration to things like foo/., c:/, \\?\Volume, etc.
*
* @param path is NUL-terminated UTF-8 path
* @return pointer inside path or path itself
*/
char *basename(const char *path) { return basename_n(path, strlen(path)); }

50
libc/conv/basename_n.c Normal file
View file

@ -0,0 +1,50 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
#include "libc/conv/isslash.h"
/**
* Returns pointer to last filename component in path.
*
* Both / and \ are are considered valid component separators on all
* platforms. Trailing slashes are ignored. We don't grant special
* consideration to things like foo/., c:/, \\?\Volume, etc.
*
* @param path is UTF-8 path
* @param size is byte length of path
* @return pointer inside path or path itself
*/
char *basename_n(const char *path, size_t size) {
size_t i, l;
if (size) {
if (isslash(path[size - 1])) {
l = size - 1;
while (isslash(path[l - 1])) --l;
if (!l) return (/*unconst*/ char *)&path[size - 1];
size = l;
}
for (i = size; i > 0; --i) {
if (isslash(path[i - 1])) {
return (/*unconst*/ char *)&path[i];
}
}
}
return (/*unconst*/ char *)path;
}

109
libc/conv/conv.h Normal file
View file

@ -0,0 +1,109 @@
#ifndef COSMOPOLITAN_LIBC_CONV_CONV_H_
#define COSMOPOLITAN_LIBC_CONV_CONV_H_
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § conversion
*/
/**
* COBOL timestamp (1601) for UNIX epoch (1970).
*
* It's a well-known fact in the social sciences that the introduction
* of the UNIX operation system signalled the end of modernity. Windows
* timestamps are living proof.
*/
#define MODERNITYSECONDS 11644473600
#define HECTONANOSECONDS 10000000
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
int abs(int) libcesque pureconst;
long labs(long) libcesque pureconst;
long long llabs(long long) libcesque pureconst;
char *ltpcpy(char *, long) paramsnonnull() libcesque nocallback;
int llog10(unsigned long) libcesque pureconst;
int atoi(const char *) paramsnonnull() libcesque nosideeffect;
long atol(const char *) paramsnonnull() libcesque nosideeffect;
long long atoll(const char *) paramsnonnull() libcesque nosideeffect;
unsigned long strtoul(const char *, char **, int) paramsnonnull((1));
long long strtoll(const char *, char **, int) paramsnonnull((1));
unsigned long long strtoull(const char *, char **, int) paramsnonnull((1));
long long strtonum(const char *, long long, long long, const char **);
intmax_t div10(intmax_t x, unsigned *rem) hidden;
intmax_t strtoimax(const char *, char **, int) paramsnonnull((1));
uintmax_t strtoumax(const char *, char **, int) paramsnonnull((1));
intmax_t wcstoimax(const wchar_t *, wchar_t **, int);
long wcstol(const wchar_t *, wchar_t **, int);
long strtol(const char *, char **, int)
paramsnonnull((1)) libcesque nosideeffect;
intmax_t __imaxabs(intmax_t) asm("imaxabs") libcesque pureconst;
#define imaxabs(x) __imaxabs(x)
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § conversion » time
*/
struct NtFileTime;
struct timespec;
struct timeval;
void filetimetotimespec(struct timespec *, struct NtFileTime) paramsnonnull();
struct NtFileTime timetofiletime(int64_t) nothrow pureconst;
int64_t filetimetotime(struct NtFileTime) nothrow pureconst;
void filetimetotimeval(struct timeval *, struct NtFileTime) nothrow;
struct NtFileTime timevaltofiletime(const struct timeval *) nosideeffect;
long convertmicros(const struct timeval *, long) paramsnonnull() nosideeffect;
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § conversion » manipulation
*/
char *basename(const char *) nosideeffect;
char *basename_n(const char *, size_t) nosideeffect;
bool isabspath(const char *) paramsnonnull() nosideeffect;
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § conversion » computation
*/
typedef struct {
int quot;
int rem;
} div_t;
typedef struct {
long int quot;
long int rem;
} ldiv_t;
typedef struct {
long long int quot;
long long int rem;
} lldiv_t;
typedef struct {
intmax_t quot;
intmax_t rem;
} imaxdiv_t;
div_t div(int, int) pureconst;
ldiv_t ldiv(long, long) pureconst;
lldiv_t lldiv(long long, long long) pureconst;
imaxdiv_t imaxdiv(intmax_t, intmax_t) pureconst;
double RoundDecimalPlaces(double, double, double(double));
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § conversion » optimizations
*/
#if __STDC_VERSION__ + 0 >= 199901L
#define div(num, den) ((div_t){(num) / (den), (num) % (den)})
#define ldiv(num, den) ((ldiv_t){(num) / (den), (num) % (den)})
#define lldiv(num, den) ((lldiv_t){(num) / (den), (num) % (den)})
#endif
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_CONV_CONV_H_ */

75
libc/conv/conv.mk Normal file
View file

@ -0,0 +1,75 @@
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
#───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘
#
# SYNOPSIS
#
# Cosmopolitan Data Conversions
#
# DESCRIPTION
#
# This package provides functions that convert between data types.
# It's intended to be lower level than the memory and formatting
# libraries.
PKGS += LIBC_CONV
LIBC_CONV_ARTIFACTS += LIBC_CONV_A
LIBC_CONV = $(LIBC_CONV_A_DEPS) $(LIBC_CONV_A)
LIBC_CONV_A = o/$(MODE)/libc/conv/conv.a
LIBC_CONV_A_FILES := $(wildcard libc/conv/*)
LIBC_CONV_A_HDRS = $(filter %.h,$(LIBC_CONV_A_FILES))
LIBC_CONV_A_SRCS_S = $(filter %.S,$(LIBC_CONV_A_FILES))
LIBC_CONV_A_SRCS_C = $(filter %.c,$(LIBC_CONV_A_FILES))
LIBC_CONV_A_SRCS = \
$(LIBC_CONV_A_SRCS_S) \
$(LIBC_CONV_A_SRCS_C)
LIBC_CONV_A_OBJS = \
$(LIBC_CONV_A_SRCS:%=o/$(MODE)/%.zip.o) \
$(LIBC_CONV_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(LIBC_CONV_A_SRCS_C:%.c=o/$(MODE)/%.o)
LIBC_CONV_A_CHECKS = \
$(LIBC_CONV_A).pkg \
$(LIBC_CONV_A_HDRS:%=o/$(MODE)/%.ok)
LIBC_CONV_A_DIRECTDEPS = \
LIBC_STUBS \
LIBC_NEXGEN32E \
LIBC_TINYMATH \
LIBC_SYSV
LIBC_CONV_A_DEPS := \
$(call uniq,$(foreach x,$(LIBC_CONV_A_DIRECTDEPS),$($(x))))
$(LIBC_CONV_A): libc/conv/ \
$(LIBC_CONV_A).pkg \
$(LIBC_CONV_A_OBJS)
$(LIBC_CONV_A).pkg: \
$(LIBC_CONV_A_OBJS) \
$(foreach x,$(LIBC_CONV_A_DIRECTDEPS),$($(x)_A).pkg)
#o/$(MODE)/libc/conv/strtoimax.o: CC = clang-10
#o/$(MODE)/libc/conv/strtoumax.o: CC = clang-10
o/$(MODE)/libc/conv/itoa64radix10.o \
o/$(MODE)/libc/conv/timetofiletime.o \
o/$(MODE)/libc/conv/filetimetotime.o \
o/$(MODE)/libc/conv/filetimetotimespec.o \
o/$(MODE)/libc/conv/filetimetotimeval.o: \
OVERRIDE_COPTS += \
-O3
LIBC_CONV_LIBS = $(foreach x,$(LIBC_CONV_ARTIFACTS),$($(x)))
LIBC_CONV_SRCS = $(foreach x,$(LIBC_CONV_ARTIFACTS),$($(x)_SRCS))
LIBC_CONV_HDRS = $(foreach x,$(LIBC_CONV_ARTIFACTS),$($(x)_HDRS))
LIBC_CONV_BINS = $(foreach x,$(LIBC_CONV_ARTIFACTS),$($(x)_BINS))
LIBC_CONV_CHECKS = $(foreach x,$(LIBC_CONV_ARTIFACTS),$($(x)_CHECKS))
LIBC_CONV_OBJS = $(foreach x,$(LIBC_CONV_ARTIFACTS),$($(x)_OBJS))
LIBC_CONV_TESTS = $(foreach x,$(LIBC_CONV_ARTIFACTS),$($(x)_TESTS))
$(LIBC_CONV_OBJS): $(BUILD_FILES) libc/conv/conv.mk
.PHONY: o/$(MODE)/libc/conv
o/$(MODE)/libc/conv: $(LIBC_CONV_CHECKS)

26
libc/conv/convertmicros.c Normal file
View file

@ -0,0 +1,26 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/calls/struct/timeval.h"
#include "libc/conv/conv.h"
#include "libc/time/time.h"
long convertmicros(const struct timeval *tv, long tick) {
return tv->tv_sec * tick + tv->tv_usec / (1000000 / tick);
}

22
libc/conv/div.c Normal file
View file

@ -0,0 +1,22 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
div_t(div)(int num, int den) { return div(num, den); }

View file

@ -0,0 +1,26 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
#include "libc/nt/struct/filetime.h"
int64_t filetimetotime(struct NtFileTime ft) {
uint64_t t = (uint64_t)ft.dwHighDateTime << 32 | ft.dwLowDateTime;
return (t - MODERNITYSECONDS * HECTONANOSECONDS) / HECTONANOSECONDS;
}

View file

@ -0,0 +1,32 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/calls/calls.h"
#include "libc/conv/conv.h"
#include "libc/nt/struct/filetime.h"
/**
* Converts Windows COBOL timestamp to UNIX epoch in nanoseconds.
*/
void filetimetotimespec(struct timespec *ts, struct NtFileTime ft) {
uint64_t t = (uint64_t)ft.dwHighDateTime << 32 | ft.dwLowDateTime;
uint64_t x = t - MODERNITYSECONDS * HECTONANOSECONDS;
ts->tv_sec = x / HECTONANOSECONDS;
ts->tv_nsec = x % HECTONANOSECONDS * 100;
}

View file

@ -0,0 +1,30 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/calls/calls.h"
#include "libc/calls/struct/timeval.h"
#include "libc/conv/conv.h"
#include "libc/nt/struct/filetime.h"
void filetimetotimeval(struct timeval *tv, struct NtFileTime ft) {
uint64_t t = (uint64_t)ft.dwHighDateTime << 32 | ft.dwLowDateTime;
uint64_t x = t - MODERNITYSECONDS * HECTONANOSECONDS;
tv->tv_sec = x / HECTONANOSECONDS;
tv->tv_usec = x % HECTONANOSECONDS / 10;
}

25
libc/conv/imaxabs.c Normal file
View file

@ -0,0 +1,25 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
#include "libc/macros.h"
intmax_t imaxabs(intmax_t x) {
return ABS(x);
}

39
libc/conv/isabspath.c Normal file
View file

@ -0,0 +1,39 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
#include "libc/conv/isslash.h"
/**
* Returns true if pathname could be absolute on any known platform.
*
* The ones we know about are System V (/foo/bar), DOS (C:\foo\bar),
* Windows NT (\\.\C:\foo\bar), Google Cloud (gs://bucket/foo/bar), etc.
*/
bool isabspath(const char *path) {
if (isslash(*path)) return true;
for (; *path; ++path) {
if (isslash(*path)) return false;
if (*path == ':') {
++path;
if (isslash(*path)) return true;
}
}
return false;
}

8
libc/conv/isslash.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef COSMOPOLITAN_LIBC_CONV_ISSLASH_H_
#define COSMOPOLITAN_LIBC_CONV_ISSLASH_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
forceinline bool isslash(int c) { return c == '/' || c == '\\'; }
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_CONV_ISSLASH_H_ */

36
libc/conv/itoa.h Normal file
View file

@ -0,0 +1,36 @@
#ifndef COSMOPOLITAN_LIBC_CONV_ITOA_H_
#define COSMOPOLITAN_LIBC_CONV_ITOA_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
/*
FASTEST + TINY
- uint64toarray_radix10(0x31337, a) l: 68 (20ns) m: 112 (33ns)
- int64toarray_radix10(0x31337, a) l: 69 (20ns) m: 134 (39ns)
FAST + AWESOME
- snprintf(a, sizeof(a), "%d", 0x31337) l: 199 (58ns) m: 421 (123ns)
- uint128toarray_radix10(0x31337, a) l: 93 (27ns) m: 141 (41ns)
- int128toarray_radix10(0x31337, a) l: 96 (28ns) m: 173 (51ns)
SLOWEST + GENERAL
- int64toarray(0x31337, a, 10) l: 218 (64ns) m: 262 (77ns)
- uint64toarray(0x31337, a, 10) l: 565 (166ns) m: 260 (76ns)
*/
size_t int128toarray_radix10(int128_t, char *);
size_t uint128toarray_radix10(uint128_t, char *);
size_t int64toarray_radix10(int64_t, char *);
size_t uint64toarray_radix10(uint64_t, char *);
size_t int64toarray(int64_t, char *, int);
size_t uint64toarray(uint64_t, char *, int);
size_t uint64toarray_radix16(uint64_t, char[hasatleast 17]);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_CONV_ITOA_H_ */

View file

@ -0,0 +1,43 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/alg/reverse.h"
#include "libc/conv/conv.h"
#include "libc/conv/itoa.h"
noinline size_t uint128toarray_radix10(uint128_t i, char *a) {
size_t j;
unsigned rem;
j = 0;
do {
i = div10(i, &rem);
a[j++] = rem + '0';
} while (i > 0);
a[j] = '\0';
reverse(a, j);
return j;
}
size_t int128toarray_radix10(int128_t i, char *a) {
if (i < 0) {
*a++ = '-';
i = -i;
}
return uint128toarray_radix10(i, a);
}

42
libc/conv/itoa64.c Normal file
View file

@ -0,0 +1,42 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/alg/reverse.h"
#include "libc/conv/conv.h"
#include "libc/conv/itoa.h"
noinline size_t uint64toarray(uint64_t i, char *a, int r) {
size_t j;
j = 0;
do {
a[j++] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i % r];
i /= r;
} while (i > 0);
a[j] = '\0';
reverse(a, j);
return j;
}
size_t int64toarray(int64_t i, char *a, int r) {
if (i < 0) {
*a++ = '-';
i = -i;
}
return uint64toarray(i, a, r);
}

View file

@ -0,0 +1,42 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/alg/reverse.h"
#include "libc/conv/conv.h"
#include "libc/conv/itoa.h"
noinline size_t uint64toarray_radix10(uint64_t i, char *a) {
size_t j;
j = 0;
do {
a[j++] = i % 10 + '0';
i /= 10;
} while (i > 0);
a[j] = '\0';
reverse(a, j);
return j;
}
size_t int64toarray_radix10(int64_t i, char *a) {
if (i < 0) {
*a++ = '-';
i = -i;
}
return uint64toarray_radix10(i, a);
}

View file

@ -0,0 +1,35 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/alg/reverse.h"
#include "libc/conv/conv.h"
size_t uint64toarray_radix16(uint64_t i, char a[hasatleast 17]) {
size_t j;
unsigned char d;
j = 0;
do {
d = i % 16;
a[j++] = d < 10 ? d + '0' : d + 'a';
i /= 16;
} while (i > 0);
a[j] = '\0';
reverse(a, j);
return j;
}

23
libc/conv/labs.c Normal file
View file

@ -0,0 +1,23 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
#include "libc/macros.h"
long(labs)(long x) { return ABS(x); }

22
libc/conv/ldiv.c Normal file
View file

@ -0,0 +1,22 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
ldiv_t(ldiv)(long num, long den) { return ldiv(num, den); }

23
libc/conv/llabs.c Normal file
View file

@ -0,0 +1,23 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
#include "libc/macros.h"
long long(llabs)(long long x) { return ABS(x); }

22
libc/conv/lldiv.c Normal file
View file

@ -0,0 +1,22 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
lldiv_t(lldiv)(long long num, long long den) { return lldiv(num, den); }

32
libc/conv/ltpcpy.c Normal file
View file

@ -0,0 +1,32 @@
/*-*- mode:c; indent-tabs-mode:nil; tab-width:2; coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/bits.h"
#include "libc/conv/conv.h"
char *ltpcpy(char *dst, long x) {
unsigned len = llog10(abs(x)) + 1;
if (x < 0) *dst++ = '-';
unsigned i = len;
do {
dst[--i] = '0' + x % 10;
x /= 10;
} while (i);
return dst + len;
}

View file

@ -0,0 +1,29 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
#include "libc/math.h"
double RoundDecimalPlaces(double f, double digs, double rounder(double)) {
if (!(0 <= digs && digs < 15)) {
return f;
} else {
return rounder(f * exp10(digs)) / exp10(digs);
}
}

28
libc/conv/sizemultiply.h Normal file
View file

@ -0,0 +1,28 @@
#ifndef COSMOPOLITAN_LIBC_CONV_SIZEMULTIPLY_H_
#define COSMOPOLITAN_LIBC_CONV_SIZEMULTIPLY_H_
#include "libc/limits.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
/**
* Multiplies memory sizes.
*
* @param count may be 0 to for realloc() free() behavior
* @param opt_out set to count*itemsize or SIZE_MAX on overflow
* @return true on success or false on overflow
*/
forceinline bool sizemultiply(size_t *opt_out, size_t count, size_t itemsize) {
size_t res = 0;
bool overflowed = false;
if (count != 0) {
res = count * itemsize;
if (((count | itemsize) & ~0xfffful) && (res / count != itemsize)) {
overflowed = true;
res = SIZE_MAX;
}
}
if (opt_out) *opt_out = res;
return !overflowed;
}
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_CONV_SIZEMULTIPLY_H_ */

16
libc/conv/strlol.h Normal file
View file

@ -0,0 +1,16 @@
#ifndef COSMOPOLITAN_LIBC_COMPAT_STRLOL_H_
#define COSMOPOLITAN_LIBC_COMPAT_STRLOL_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#define STRLOL(STR, ENDPTR, OPTIONAL_BASE, MIN, MAX) \
({ \
intmax_t res = strtoimax(STR, ENDPTR, OPTIONAL_BASE); \
if (res < MIN) return MIN; \
if (res > MAX) return MAX; \
res; \
})
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_COMPAT_STRLOL_H_ */

109
libc/conv/strtoimax.c Normal file
View file

@ -0,0 +1,109 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
#include "libc/limits.h"
#include "libc/nexgen32e/bsr.h"
#include "libc/str/str.h"
/**
* Decodes 128-bit signed integer from ASCII string.
*
* @param s is a non-NULL NUL-terminated string
* @param endptr if non-NULL will always receive a pointer to the char
* following the last one this function processed, which is usually
* the NUL byte, or in the case of invalid strings, would point to
* the first invalid character
* @param base can be anywhere between [2,36] or 0 to auto-detect based
* on the the prefixes 0 (octal), 0x (hexadecimal), 0b (binary), or
* decimal (base 10) by default.
* @return the decoded saturated number
* @see strtoumax
*/
intmax_t strtoimax(const char *s, char **endptr, int base) {
bool neg;
uintmax_t x;
intmax_t res;
unsigned diglet, bits;
x = 0;
bits = 0;
neg = false;
while (isspace(*s)) {
s++;
}
switch (*s) {
case '-':
neg = true;
/* 𝑠𝑙𝑖𝑑𝑒 */
case '+':
s++;
break;
default:
break;
}
if (!(2 <= base && base <= 36)) {
if (*s == '0') {
s++;
if (*s == 'x' || *s == 'X') {
s++;
base = 16;
} else if (*s == 'b' || *s == 'B') {
s++;
base = 2;
} else {
base = 8;
}
} else {
base = 10;
}
}
for (;;) {
diglet = kBase36[*s & 0xff];
if (!diglet || diglet > base) break;
diglet -= 1;
if (!diglet || !x || (bits = bsr(diglet) + bsrmax(x)) < 127) {
s++;
x *= base;
x += diglet;
} else if (neg) {
if (bits == 127) {
x *= base;
x += diglet;
if (x == INTMAX_MIN) s++;
}
x = INTMAX_MIN;
break;
} else {
x = INTMAX_MAX;
break;
}
}
if (endptr) {
*endptr = s;
}
res = x;
return neg ? -res : res;
}

31
libc/conv/strtol.c Normal file
View file

@ -0,0 +1,31 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
#include "libc/conv/strlol.h"
#include "libc/limits.h"
/**
* Converts string to number.
*
* @param optional_base is recommended as 0 for flexidecimal
*/
long strtol(const char *s, char **opt_out_end, int optional_base) {
return STRLOL(s, opt_out_end, optional_base, LONG_MIN, LONG_MAX);
}

26
libc/conv/strtoll.c Normal file
View file

@ -0,0 +1,26 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
#include "libc/conv/strlol.h"
#include "libc/limits.h"
long long strtoll(const char *s, char **endptr, int optional_base) {
return STRLOL(s, endptr, optional_base, LONG_LONG_MIN, LONG_LONG_MAX);
}

38
libc/conv/strtonum.c Normal file
View file

@ -0,0 +1,38 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
#include "libc/str/str.h"
#include "libc/sysv/errfuns.h"
/**
* Converts string to integer, the BSD way.
*/
long long strtonum(const char *nptr, long long minval, long long maxval,
const char **errstr) {
char *end;
intmax_t res = strtoimax(nptr, &end, 10);
if (res < minval || res > maxval || end != nptr + strlen(nptr)) {
if (errstr) *errstr = "bad number";
return einval();
} else {
if (errstr) *errstr = NULL;
return res;
}
}

26
libc/conv/strtoul.c Normal file
View file

@ -0,0 +1,26 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
#include "libc/conv/strlol.h"
#include "libc/limits.h"
unsigned long strtoul(const char *s, char **endptr, int optional_base) {
return STRLOL(s, endptr, optional_base, ULONG_MIN, ULONG_MAX);
}

26
libc/conv/strtoull.c Normal file
View file

@ -0,0 +1,26 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
#include "libc/conv/strlol.h"
#include "libc/limits.h"
unsigned long long strtoull(const char *s, char **endptr, int optional_base) {
return STRLOL(s, endptr, optional_base, ULONG_LONG_MIN, ULONG_LONG_MAX);
}

64
libc/conv/strtoumax.c Normal file
View file

@ -0,0 +1,64 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
#include "libc/str/str.h"
/**
* Decodes 128-bit unsigned integer from ASCII string.
*
* This is a more restricted form of strtoimax() that's useful for folks
* needing to decode numbers in the range [1^127, 1^128).
*/
uintmax_t strtoumax(const char *s, char **endptr, int base) {
const unsigned char *p = (const unsigned char *)s;
uintmax_t res = 0;
while (isspace(*p)) {
p++;
}
if (!base) {
if (*p == '0') {
p++;
if (*p == 'x' || *p == 'X') {
p++;
base = 16;
} else if (*p == 'b' || *p == 'B') {
p++;
base = 2;
} else {
base = 8;
}
} else {
base = 10;
}
}
for (;;) {
unsigned diglet = kBase36[*p];
if (!diglet || diglet > base) break;
p++;
res *= base;
res += diglet - 1;
}
if (endptr) *endptr = (char *)p;
return res;
}

View file

@ -0,0 +1,26 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
#include "libc/nt/struct/filetime.h"
struct NtFileTime timetofiletime(int64_t t) {
uint64_t t2 = (t + MODERNITYSECONDS) * HECTONANOSECONDS;
return (struct NtFileTime){(uint32_t)t2, (uint32_t)(t2 >> 32)};
}

View file

@ -0,0 +1,29 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/calls/struct/timeval.h"
#include "libc/conv/conv.h"
#include "libc/nt/struct/filetime.h"
#include "libc/time/time.h"
struct NtFileTime timevaltofiletime(const struct timeval *tv) {
uint64_t t2 = tv->tv_sec * HECTONANOSECONDS + tv->tv_usec * 10 +
MODERNITYSECONDS * HECTONANOSECONDS;
return (struct NtFileTime){(uint32_t)t2, (uint32_t)(t2 >> 32)};
}

16
libc/conv/wcslol.h Normal file
View file

@ -0,0 +1,16 @@
#ifndef COSMOPOLITAN_LIBC_CONV_WCSLOL_H_
#define COSMOPOLITAN_LIBC_CONV_WCSLOL_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#define WCSLOL(STR, ENDPTR, OPTIONAL_BASE, MIN, MAX) \
({ \
intmax_t res = wcstoimax(STR, ENDPTR, OPTIONAL_BASE); \
if (res < MIN) return MIN; \
if (res > MAX) return MAX; \
res; \
})
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_CONV_WCSLOL_H_ */

69
libc/conv/wcstoimax.c Normal file
View file

@ -0,0 +1,69 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
#include "libc/str/str.h"
intmax_t wcstoimax(const wchar_t *s, wchar_t **endptr, int base) {
intmax_t res = 0;
int neg = 0;
while (iswspace(*s)) {
s++;
}
switch (*s) {
case '-':
neg = 1;
/* fallthrough */
case '+':
s++;
break;
default:
break;
}
if (!base) {
if (*s == '0') {
s++;
if (*s == 'x' || *s == 'X') {
s++;
base = 16;
} else if (*s == 'b' || *s == 'B') {
s++;
base = 2;
} else {
base = 8;
}
} else {
base = 10;
}
}
for (;;) {
unsigned diglet = kBase36[*s];
if (!diglet || diglet > base) break;
s++;
res *= base; /* needs __muloti4() w/ clang */
res -= diglet - 1;
}
if (endptr) *endptr = (wchar_t *)s;
return neg ? res : -res;
}

27
libc/conv/wcstol.c Normal file
View file

@ -0,0 +1,27 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/conv/conv.h"
#include "libc/conv/wcslol.h"
#include "libc/limits.h"
#include "libc/str/str.h"
long wcstol(const wchar_t *s, wchar_t **end, int opt_base) {
return WCSLOL(s, end, opt_base, LONG_MIN, LONG_MAX);
}