Initial commit
This commit is contained in:
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) [year] [fullname]
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
76
Makefile
Normal file
76
Makefile
Normal file
@@ -0,0 +1,76 @@
|
||||
# Compiler Flags
|
||||
CC := gcc
|
||||
CFLAGS := -g -Wall -Wextra -Werror -pedantic -fsanitize=address,undefined -fno-omit-frame-pointer
|
||||
|
||||
# Directory variables
|
||||
LIBDIR := lib
|
||||
OBJ := obj
|
||||
INC := include
|
||||
SRC := src
|
||||
TEST := tests
|
||||
|
||||
# Filepath Pattern Matching
|
||||
LIB := $(LIBDIR)/lib.a
|
||||
SRCS := $(wildcard $(SRC)/*.c)
|
||||
OBJS := $(patsubst $(SRC)/%.c, $(OBJ)/%.o, $(SRCS))
|
||||
TESTS := $(wildcard $(TEST)/*.c)
|
||||
TESTBINS := $(patsubst $(TEST)/%.c, $(TEST)/bin/%, $(TESTS))
|
||||
|
||||
# Commands must be labeled PHONY
|
||||
.PHONY: all release clean test
|
||||
|
||||
# Compiler Release Flags
|
||||
release: CFLAGS := -Wall -Wextra -Werror -pedantic -fsanitize=address,undefined -fno-omit-frame-pointer -O2 -DNDEBUG
|
||||
release: clean $(LIB)
|
||||
|
||||
# Target for compilation.
|
||||
all: $(LIB)
|
||||
|
||||
# Target / Dependencies
|
||||
$(LIB): $(OBJS) | $(LIBDIR)
|
||||
$(RM) $(LIB)
|
||||
ar -cvrs $@ $^
|
||||
|
||||
$(OBJ)/%.o: $(SRC)/%.c $(SRC)/%.h | $(OBJ)
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
$(OBJ)/%.o: $(SRC)/%.c | $(OBJ)
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
$(TEST)/bin/%: $(TEST)/%.c $(LIB) | $(TEST)/bin
|
||||
$(CC) $(CFLAGS) $< $(LIB) -o $@
|
||||
|
||||
# Make directories if none.
|
||||
$(LIBDIR):
|
||||
mkdir $@
|
||||
|
||||
$(INC):
|
||||
mkdir $@
|
||||
|
||||
$(OBJ):
|
||||
mkdir $@
|
||||
|
||||
$(TEST)/bin:
|
||||
mkdir $@
|
||||
|
||||
# Run the tests in the bin folder and track results
|
||||
test: $(LIB) $(TEST)/bin $(TESTBINS)
|
||||
@SUCCESS_COUNT=0; FAILURE_COUNT=0; \
|
||||
for test in $(TESTBINS); do \
|
||||
./$$test; \
|
||||
EXIT_CODE=$$?; \
|
||||
TEST_NAME=$(notdir $$test); \
|
||||
if [ $$EXIT_CODE -eq 0 ]; then \
|
||||
echo "\033[0;32m$$TEST_NAME: EXIT CODE: $$EXIT_CODE (SUCCESS)\033[0m"; \
|
||||
SUCCESS_COUNT=$$((SUCCESS_COUNT + 1)); \
|
||||
else \
|
||||
echo "\033[0;31m$$TEST_NAME: EXIT CODE: $$EXIT_CODE (FAILURE)\033[0m"; \
|
||||
FAILURE_COUNT=$$((FAILURE_COUNT + 1)); \
|
||||
fi; \
|
||||
done; \
|
||||
echo "\n\nTests completed"; \
|
||||
echo "SUCCESS: $$SUCCESS_COUNT"; \
|
||||
echo "FAILURE: $$FAILURE_COUNT";
|
||||
|
||||
clean:
|
||||
$(RM) -r $(LIBDIR) $(OBJ) $(TEST)/bin/
|
||||
28
README.md
Normal file
28
README.md
Normal file
@@ -0,0 +1,28 @@
|
||||
# sv
|
||||
|
||||
## Description
|
||||
String View implementation written in c.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Description](#description)
|
||||
- [Features](#features)
|
||||
- [Usage](#usage)
|
||||
- [Credits / Resources](#credits--resources)
|
||||
- [License](#license)
|
||||
|
||||
## Features / TODOS
|
||||
+ Easy strings.
|
||||
+ Safe access.
|
||||
+ Clear semantics
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
## Credits / Resources
|
||||
[Mongoose Webserver](https://mongoose.ws/)<br>
|
||||
[Tsoding SV](https://github.com/tsoding/sv)<br>
|
||||
[C Interfaces and Implementations - David Hanson](https://github.com/drh/cii)<br>
|
||||
|
||||
## License
|
||||
This project is licensed under MIT - see the [LICENSE](LICENSE) file for details.
|
||||
18
include/sv.h
Normal file
18
include/sv.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef SV_INCLUDED
|
||||
#define SV_INCLUDED
|
||||
|
||||
#include <stddef.h>
|
||||
struct String_View{
|
||||
size_t len;
|
||||
const char *buf;
|
||||
};
|
||||
typedef struct String_View SV;
|
||||
|
||||
// printf macros for String_View
|
||||
#define SV_FMT "%.*s"
|
||||
#define SV_ARG(sv) (int) (sv).len, (sv).buf
|
||||
|
||||
extern struct String_View sv_str(const char *s);
|
||||
extern struct String_View sv_strn(const char *s, size_t n);
|
||||
|
||||
#endif
|
||||
14
src/sv.c
Normal file
14
src/sv.c
Normal file
@@ -0,0 +1,14 @@
|
||||
#include "../include/sv.h"
|
||||
#include <string.h>
|
||||
|
||||
struct String_View sv_str(const char *s) {
|
||||
SV str = {s == NULL ? 0 : strlen(s), (char *) s };
|
||||
return str;
|
||||
}
|
||||
|
||||
struct String_View sv_strn(const char *s, size_t n) {
|
||||
SV str = {n, (char *) s};
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
15
tests/01_sv_str.c
Normal file
15
tests/01_sv_str.c
Normal file
@@ -0,0 +1,15 @@
|
||||
#define DEBUG
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include "../include/sv.h"
|
||||
|
||||
int main(void){
|
||||
const char *s = "Hello World!";
|
||||
SV sv = sv_str(s);
|
||||
|
||||
assert(sv.buf != NULL);
|
||||
assert(strlen(s) == sv.len);
|
||||
assert( memcmp(sv.buf, s, strlen(s) ) == 0);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
15
tests/02_sv_strn.c
Normal file
15
tests/02_sv_strn.c
Normal file
@@ -0,0 +1,15 @@
|
||||
#define DEBUG
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include "../include/sv.h"
|
||||
|
||||
int main(void){
|
||||
const char *s = "Hello World!";
|
||||
SV sv = sv_strn(s, strlen(s));
|
||||
|
||||
assert(sv.buf != NULL);
|
||||
assert(strlen(s) == sv.len);
|
||||
assert( memcmp(sv.buf, s, strlen(s) ) == 0);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user