Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
sudo: required
dist: trusty
language: c
language: cpp
os:
- linux
- osx
Expand Down
7 changes: 3 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@
#

cmake_minimum_required(VERSION 2.8)
project(native)

# Native is written in pure C
enable_language(C)
project(native C CXX)

# Only enable GNU Assembly for Linux amd64
# Please careful when you would like to optimize any function in ASM
Expand Down Expand Up @@ -72,6 +69,7 @@ file(GLOB_RECURSE SOURCES
crypto/*
datetime/*
general/*
generic/*
network/*
server/*
storage/*
Expand All @@ -93,6 +91,7 @@ file(GLOB_RECURSE TESTS
crypto/*_test.c
datetime/*_test.c
general/*_test.c
generic/*_test.cpp
network/*_test.c
server/*_test.c
storage/*_test.c
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ We use this library in our production and unit test with Travis CI so it just wo

This project is also useful for new developers in practical programming

### Standard Library
### Standard C Library
- Generic functions (length, append, join, segment, sort, search)
- Compression (gzip)
- Cryptography (md5, sha1, base64)
Expand All @@ -24,6 +24,11 @@ This project is also useful for new developers in practical programming
- Data Type (json)
- Third Party (Twilio, SendGrid, Etcd, ElasticSearch, Stripe, Firebase)

### Generic C++ Functions (generic.h)
- type, ord, chr, max, min, round
- str, int, short, long, float, double
- len, slice, sorted, reverse, range, sum

### Road map
- Inherit good builtin functions from PHP, Python, Ruby, NodeJS, Perl
- Develop fundamental algorithms and advanced data structure
Expand Down
2 changes: 1 addition & 1 deletion builtin.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@
#include "validator.h"
#include "vendor.h"

#endif //BUILTIN_BUILTIN_H
#endif //NATIVE_BUILTIN_H


4 changes: 2 additions & 2 deletions datetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef BUILTIN_DATETIME_H
#define BUILTIN_DATETIME_H
#ifndef NATIVE_DATETIME_H
#define NATIVE_DATETIME_H
#ifdef __linux__
#include <stdint.h>
#elif __APPLE__
Expand Down
6 changes: 3 additions & 3 deletions general.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
char **append_pointer_char(char **target, char *append);

char *join_pointer_pointer_char(char **target);
char *join_delim_pointer_pointer_char(char **target, const char *delim);
char *join_delimiter_pointer_pointer_char(char **target, const char *delimiter);

int length_pointer_char(char *target);
int length_pointer_pointer_char(char **target);
Expand All @@ -43,8 +43,8 @@ int length_float(float target);

char *segment_pointer_char(char *target, int from, int to);
char **segment_pointer_pointer_char(char **target, int from, int to);
int linear_search(int array[], int lenght, int key);
int binary_search(int array[], int lenght, int key);
int linear_search(int array[], int length, int key);
int binary_search(int array[], int length, int key);

void sort_int(int *array, int begin_array, int end_array);
void sort_float(float *array, int begin_array, int end_array);
Expand Down
18 changes: 9 additions & 9 deletions general/append_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@

TEST(General, AppendPointerChar) {
char *target[] = {
(char *) "The",
(char *) "quick",
(char *) "brown",
(char *) "fox",
(char *) "jumps",
(char *) "over",
(char *) "the",
(char *) "lazy",
'\0'
(char *) "The",
(char *) "quick",
(char *) "brown",
(char *) "fox",
(char *) "jumps",
(char *) "over",
(char *) "the",
(char *) "lazy",
'\0'
};
char *append = "dog";
ASSERT_EQUAL(8, length_pointer_pointer_char(target));
Expand Down
31 changes: 14 additions & 17 deletions general/join.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,29 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../type.h"
#include "../general.h"

#define MAX_SIZE 100000

/**
* Join pointer pointer char
* Concatenating all elements in target array into single string
*
* @param target
* @return char pointer
*/
inline char *join_pointer_pointer_char(char **target)
{
inline char *join_pointer_pointer_char(char **target) {
register char **pointer;
register int total_length = 0, item_length = 0;
char *result_tmp = calloc(MAX_SIZE, sizeof(char));
char *result_tmp = calloc(MAX_STRING_LENGTH, sizeof(char));
for (pointer = target; *pointer; ++pointer) {
item_length = length_pointer_char(*pointer);
memcpy(result_tmp + total_length, *pointer, item_length);
total_length += item_length;
}
// Deallocate memory
// Allocate enough memory for result
char *result = calloc(total_length + 1, sizeof(char));
memcpy(result, result_tmp, total_length);
// Saving memory
// Free memory for temporary variable
free(result_tmp);
return result;
}
Expand All @@ -63,24 +61,23 @@ inline char *join_pointer_pointer_char(char **target)
* @param target
* @return char pointer
*/
inline char *join_delim_pointer_pointer_char(char **target, const char *delim)
{
inline char *join_delimiter_pointer_pointer_char(char **target, const char *delimiter) {
register char **pointer;
register int total_length = 0, item_length = 0;
int delim_length = length_pointer_char((char*) delim);
char *result_tmp = calloc(MAX_SIZE, sizeof(char));
int delimiter_length = length_pointer_char((char*) delimiter);
char *result_tmp = calloc(MAX_STRING_LENGTH, sizeof(char));
for (pointer = target; *pointer; ++pointer) {
item_length = length_pointer_char(*pointer);
memcpy(result_tmp + total_length, *pointer, item_length);
total_length += item_length;
memcpy(result_tmp + total_length, delim, delim_length);
total_length += delim_length;
memcpy(result_tmp + total_length, delimiter, delimiter_length);
total_length += delimiter_length;
}
// Deallocate memory
char *result = calloc(total_length - delim_length + 1, sizeof(char));
// Allocate enough memory for result
char *result = calloc(total_length - delimiter_length + 1, sizeof(char));
// Copy and remove remainder delimiter
memcpy(result, result_tmp, total_length - delim_length);
// Saving memory
memcpy(result, result_tmp, total_length - delimiter_length);
// Free memory for temporary variable
free(result_tmp);
return result;
}
47 changes: 24 additions & 23 deletions general/join_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,36 @@

TEST(General, JoinPointerPointerChar) {
char *target[] = {
(char *) "The",
(char *) "quick",
(char *) "brown",
(char *) "fox",
(char *) "jumps",
(char *) "over",
(char *) "the",
(char *) "lazy",
(char *) "dog",
'\0'
(char *) "The",
(char *) "quick",
(char *) "brown",
(char *) "fox",
(char *) "jumps",
(char *) "over",
(char *) "the",
(char *) "lazy",
(char *) "dog",
'\0'
};
char *expect = "Thequickbrownfoxjumpsoverthelazydog";
ASSERT_STR(expect, join_pointer_pointer_char(target));
}

TEST(General, JoinDelimPointerPointerChar) {
TEST(General, JoinDelimiterPointerPointerChar) {
char *target[] = {
(char *) "The",
(char *) "quick",
(char *) "brown",
(char *) "fox",
(char *) "jumps",
(char *) "over",
(char *) "the",
(char *) "lazy",
(char *) "dog",
'\0'
(char *) "The",
(char *) "quick",
(char *) "brown",
(char *) "fox",
(char *) "jumps",
(char *) "over",
(char *) "the",
(char *) "lazy",
(char *) "dog",
'\0'
};
char *delim = "|";

char *delimiter = "|";
char *expect = "The|quick|brown|fox|jumps|over|the|lazy|dog";
ASSERT_STR(expect, join_delim_pointer_pointer_char(target, delim));
ASSERT_STR(expect, join_delimiter_pointer_pointer_char(target, delimiter));
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 10 additions & 12 deletions general/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

//Linear Search
#define NOT_FOUND -1

/**
Expand All @@ -38,8 +37,8 @@
*/
int linear_search(int array[], int length, int key) {
register int index;
for(index = 0; index < length; index++) {
if(array[index] == key) {
for (index = 0; index < length; index++) {
if (array[index] == key) {
return index;
}
}
Expand All @@ -56,19 +55,18 @@ int linear_search(int array[], int length, int key) {
* @return result
*/
int binary_search(int array[], int length, int key) {
int index;
int left, right;
register int middle, left, right;
left = 0;
right = length;
while(left <= right) {
index = (left + right )/2;
if(key == array[index]) {
return index;
while (left <= right) {
middle = (left + right ) / 2;
if (key == array[middle]) {
return middle;
}
if(key < array[index]) {
right = index - 1;
if (key < array[middle]) {
right = middle - 1;
} else {
left = index + 1;
left = middle + 1;
}
}
return NOT_FOUND;
Expand Down
File renamed without changes.
File renamed without changes.
44 changes: 44 additions & 0 deletions generic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright (c) 2016 Food Tiny Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
* Generic functions inspired from Python Builtin Functions
*
* We feel that Python has an awesome builtin functions so we would like to
* rewrite it in C++ using Native Library
*
* Thanks to Python Authors
* https://docs.python.org/2/library/functions.html
* © Copyright 1990-2017, Python Software Foundation
* All right reserve.
*/

#ifndef NATIVE_GENERIC_H
#define NATIVE_GENERIC_H

template <typename T> int len(T target);

#endif //NATIVE_GENERIC_H
25 changes: 25 additions & 0 deletions generic/abs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) 2016 Food Tiny Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
27 changes: 27 additions & 0 deletions generic/chr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (c) 2016 Food Tiny Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


Loading