diff --git a/libc/mem/shuffle.internal.h b/libc/mem/shuffle.internal.h deleted file mode 100644 index 2b543a89d..000000000 --- a/libc/mem/shuffle.internal.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef COSMOPOLITAN_LIBC_RAND_SHUFFLE_H_ -#define COSMOPOLITAN_LIBC_RAND_SHUFFLE_H_ -#include "libc/intrin/xchg.h" - -/** - * Fisher-Yates shuffle. - * - * @param R is a function like rand() → ≥0 - * @param A is a typed array - * @param n is the number of items in A - * @see ARRAYLEN() - */ -#define shuffle(R, A, n) \ - do { \ - autotype(A) Array = (A); \ - for (size_t i = (n) - 1; i >= 1; --i) { \ - xchg(&Array[i], &Array[R() % (i + 1)]); \ - } \ - } while (0) - -#endif /* COSMOPOLITAN_LIBC_RAND_SHUFFLE_H_ */ diff --git a/libc/stdio/getdelim_unlocked.c b/libc/stdio/getdelim_unlocked.c index 44a1f156b..569040836 100644 --- a/libc/stdio/getdelim_unlocked.c +++ b/libc/stdio/getdelim_unlocked.c @@ -44,9 +44,8 @@ ssize_t getdelim_unlocked(char **s, size_t *n, int delim, FILE *f) { *n = 0; for (i = 0;; i += m) { m = f->end - f->beg; - if ((p = memchr(f->buf + f->beg, delim, m))) { + if ((p = memchr(f->buf + f->beg, delim, m))) m = p + 1 - (f->buf + f->beg); - } if (i + m + 1 > *n) { n2 = i + m + 1; s2 = realloc(*s, n2); @@ -59,10 +58,9 @@ ssize_t getdelim_unlocked(char **s, size_t *n, int delim, FILE *f) { } } memcpy(*s + i, f->buf + f->beg, m); - (*s)[i + m] = '\0'; - if ((f->beg += m) == f->end) { + (*s)[i + m] = 0; + if ((f->beg += m) == f->end) f->beg = f->end = 0; - } if (p) { return i + m; } else if (f->fd == -1) { @@ -71,7 +69,7 @@ ssize_t getdelim_unlocked(char **s, size_t *n, int delim, FILE *f) { if (!rc) break; f->end = rc; - } else if (errno != EINTR) { + } else { f->state = errno; return -1; } diff --git a/libc/stdio/strfry.c b/libc/stdio/strfry.c index eac05107d..56a703cbd 100644 --- a/libc/stdio/strfry.c +++ b/libc/stdio/strfry.c @@ -16,14 +16,23 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/mem/shuffle.internal.h" #include "libc/stdio/rand.h" #include "libc/str/str.h" /** - * Jumbles up string. + * Performs Fisher-Yates shuffle on string in-place to create anagram. + * + * This implementation uses rand() so `srand(time(0))` may be desired. */ char *strfry(char *s) { - shuffle(rand, s, strlen(s)); + size_t i = strlen(s); + while (i > 1) { + size_t x = rand(); + size_t y = rand(); + size_t j = ((x << 31) ^ y) % i--; + char t = s[j]; + s[j] = s[i]; + s[i] = t; + } return s; } diff --git a/test/libc/stdio/strfry_test.c b/test/libc/stdio/strfry_test.c new file mode 100644 index 000000000..a87ee54ba --- /dev/null +++ b/test/libc/stdio/strfry_test.c @@ -0,0 +1,38 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2024 Justine Alexandra Roberts Tunney │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/stdio/rand.h" +#include "libc/testlib/testlib.h" + +TEST(strfry, empty) { + char s[1] = ""; + EXPECT_EQ(s, strfry(s)); + EXPECT_STREQ("", s); +} + +TEST(strfry, one) { + char s[2] = "a"; + EXPECT_EQ(s, strfry(s)); + EXPECT_STREQ("a", s); +} + +TEST(strfry, test) { + char s[5] = "abcd"; + EXPECT_EQ(s, strfry(s)); + EXPECT_STREQ("cbda", s); +} diff --git a/test/libc/x/utf8to32_test.c b/test/libc/x/utf8to32_test.c index cf17662bb..5f54e2d17 100644 --- a/test/libc/x/utf8to32_test.c +++ b/test/libc/x/utf8to32_test.c @@ -18,7 +18,6 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/mem/gc.h" #include "libc/mem/mem.h" -#include "libc/mem/shuffle.internal.h" #include "libc/stdio/rand.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/hyperion.h" @@ -82,11 +81,20 @@ TEST(utf32to8, testLargeThompsonPikeEncoded) { -1, 0))); } +void shuffle(wchar_t *a, int n) { + for (int i = n - 1; i >= 1; --i) { + int r = rand() % (i + 1); + wchar_t t = a[r]; + a[r] = a[i]; + a[i] = t; + } +} + char *GenerateBranchyUtf8Text(size_t *out_n) { char *p; size_t n; wchar_t *q = gc(utf8to32(kViewables, kViewablesSize, &n)); - shuffle(lemur64, q, n); + shuffle(q, n); p = utf32to8(q, n, &n); if (out_n) *out_n = n;