Fix examples based on older versions, add build examples script
This commit is contained in:
parent
f4d4a8ab00
commit
48218b0cdb
7 changed files with 38 additions and 16 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
a.out
|
||||||
|
examples/*
|
||||||
|
!examples/*.c
|
||||||
4
buildExamples.sh
Executable file
4
buildExamples.sh
Executable file
|
|
@ -0,0 +1,4 @@
|
||||||
|
#!/bin/sh
|
||||||
|
for f in examples/*.c; do
|
||||||
|
cc -O2 -Wall -Wextra -std=c99 "$f" -o "${f%.c}"
|
||||||
|
done
|
||||||
|
|
@ -1,16 +1,17 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stddef.h>
|
||||||
#define LAZY_IMPL
|
#define LAZY_IMPL
|
||||||
#include "lazy.h"
|
#include "../lazy.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Lz_DA(float) nums = {0};
|
Lz_DA(float) nums = {0};
|
||||||
lz_da_reserve(nums, 64);
|
lz_da_reserve(nums, 64);
|
||||||
for (int i = 0; i < 32; ++i) {
|
for (size_t i = 0; i < 32; ++i) {
|
||||||
lz_da_append(nums, 2.0*i);
|
lz_da_append(nums, 2.0*i);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < nums.cnt; ++i) {
|
for (size_t i = 0; i < nums.cnt; ++i) {
|
||||||
printf("%f\n", nums.elts[i]);
|
printf("%f\n", nums.data[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stddef.h>
|
||||||
#define LAZY_IMPL
|
#define LAZY_IMPL
|
||||||
#include "../lazy.h"
|
#include "../lazy.h"
|
||||||
|
|
||||||
|
|
@ -9,14 +10,14 @@ int main() {
|
||||||
lz_da_append(xs, 4);
|
lz_da_append(xs, 4);
|
||||||
lz_da_shrink_to_fit(xs);
|
lz_da_shrink_to_fit(xs);
|
||||||
|
|
||||||
for (int i = 0; i < xs.cnt; ++i) {
|
for (size_t i = 0; i < xs.cnt; ++i) {
|
||||||
printf("%d: %d\n", i, xs.elts[i]);
|
printf("%zu: %d\n", i, xs.data[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
lz_da_insert(xs,1,2);
|
lz_da_insert(xs,1,2);
|
||||||
|
|
||||||
for (int i = 0; i < xs.cnt; ++i) {
|
for (size_t i = 0; i < xs.cnt; ++i) {
|
||||||
printf("%d: %d\n", i, xs.elts[i]);
|
printf("%zu: %d\n", i, xs.data[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
#define LAZY_IMPL
|
#define LAZY_IMPL
|
||||||
#include "lazy.h"
|
#include "../lazy.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
@ -7,16 +7,16 @@
|
||||||
int isnotalpha(int x) { return !isalpha(x); }
|
int isnotalpha(int x) { return !isalpha(x); }
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
char *src = strdup("This is a list of words, which apparently has been referred to previously as a sentence")
|
char *src = lz_cstr_dup("This is a list of words, which apparently has been referred to previously as a sentence");
|
||||||
Lz_Slc s = lz_cstr_to_slc(src);
|
Lz_Slc s = lz_cstr_to_slc(src);
|
||||||
|
|
||||||
Lz_Slc word;
|
Lz_Slc word;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
do {
|
do {
|
||||||
lz_split_slc_typ(&s, isnotalpha, &word);
|
lz_split_slc_typ(&s, isnotalpha, &word);
|
||||||
printf("Word %d: "LZ_SLC_FMT"\n", i, LZ_SLC_PRINTF(word));
|
printf("Word %d: "LZ_STR_FMT"\n", i, LZ_STR_PRINTF(word));
|
||||||
++i;
|
++i;
|
||||||
} while(s.len != 0);
|
} while(s.cnt != 0);
|
||||||
|
|
||||||
free(src);
|
free(src);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,16 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <string.h>
|
||||||
#define LAZY_IMPL
|
#define LAZY_IMPL
|
||||||
#include "lazy.h"
|
#include "../lazy.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
char *src = strdup(" \t This text has some extra whitespace \t ");
|
char *src = lz_cstr_dup(" \t This text has some extra whitespace \t ");
|
||||||
Lz_Slc s = lz_cstr_to_slc(src);
|
Lz_Slc s = lz_cstr_to_slc(src);
|
||||||
|
|
||||||
printf("|"LZ_SLC_FMT"|\n", LZ_SLC_PRINTF(s));
|
printf("|"LZ_STR_FMT"|\n", LZ_STR_PRINTF(s));
|
||||||
lz_trim_typ(&s, isspace);
|
lz_trim_typ(&s, isspace);
|
||||||
printf("|"LZ_SLC_FMT"|\n", LZ_SLC_PRINTF(s));
|
printf("|"LZ_STR_FMT"|\n", LZ_STR_PRINTF(s));
|
||||||
|
|
||||||
free(src); // excessive but this is how you should do it
|
free(src); // excessive but this is how you should do it
|
||||||
|
|
||||||
|
|
|
||||||
12
lazy.h
12
lazy.h
|
|
@ -19,6 +19,7 @@ void lz_drop_right(Lz_Slc *src, size_t n);
|
||||||
void lz_split_slc(Lz_Slc *src, char delim, Lz_Slc *dst);
|
void lz_split_slc(Lz_Slc *src, char delim, Lz_Slc *dst);
|
||||||
void lz_split_slc_typ(Lz_Slc *src, int (*classfunc)(int), Lz_Slc *dst);
|
void lz_split_slc_typ(Lz_Slc *src, int (*classfunc)(int), Lz_Slc *dst);
|
||||||
Lz_Slc lz_cstr_to_slc(char *dat);
|
Lz_Slc lz_cstr_to_slc(char *dat);
|
||||||
|
char *lz_cstr_dup(const char *s);
|
||||||
|
|
||||||
#define Lz_DA(T) struct {T *data; size_t cnt; size_t cap; }
|
#define Lz_DA(T) struct {T *data; size_t cnt; size_t cap; }
|
||||||
#define lz_da_append(da, x) do { \
|
#define lz_da_append(da, x) do { \
|
||||||
|
|
@ -85,6 +86,17 @@ Lz_Slc lz_cstr_to_slc(char *dat);
|
||||||
typedef Lz_DA(char) Lz_SB;
|
typedef Lz_DA(char) Lz_SB;
|
||||||
|
|
||||||
#ifdef LAZY_IMPL
|
#ifdef LAZY_IMPL
|
||||||
|
|
||||||
|
// Horribly enough, strdup is a gnu extension
|
||||||
|
// Here's a portable implementation
|
||||||
|
char *lz_cstr_dup(const char *s) {
|
||||||
|
if (!s) return NULL;
|
||||||
|
size_t n = strlen(s);
|
||||||
|
char *p = malloc((n+1)*sizeof(char));
|
||||||
|
if (!p) return NULL;
|
||||||
|
memcpy(p, s, n+1);
|
||||||
|
return p;
|
||||||
|
}
|
||||||
void lz_drop_left(Lz_Slc *src, size_t n) {
|
void lz_drop_left(Lz_Slc *src, size_t n) {
|
||||||
if (src->cnt < n) n = src->cnt;
|
if (src->cnt < n) n = src->cnt;
|
||||||
src->data += n;
|
src->data += n;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue