added hedley for more portable inline and non-null specifiers, changed some prefix to postfix incr/decr(in hope that the compiler optimizes accordingly), refactored some functions, added string_find_str(), reworked linked list header and added implementation, added memswap(), made ntoh32 more portable

AuthorKonata <konata@posteo.jp>
Date
Commit858bbe01da35ae1aa786a8f73eeeb8bd2b4bf5df
Parent1cc1a61
15 files changed, 1233 insertions(+), 21 deletions(-)
▾Minclude/cutils/byte_array.h
@@ -30,6 +30,7 @@ void bytearray_remove(Bytearray* bytearray, size_t index, void (*rmv)(void*));
bool_t bytearray_adjust_size(Bytearray* bytearray, size_t size);
bool_t bytearray_shrink(Bytearray* bytearray);
HEDLEY_NON_NULL(3)
size_t* bytearray_find(const Bytearray* haystack, const void* needle, int (*cmp)(const void*, const void*));
▾Minclude/cutils/common.h
@@ -2,6 +2,7 @@
#define CUTILS_COMMON_H
#include <stddef.h>
#include <cutils/hedley.h>
#ifndef SIZE_MAX
#define SIZE_MAX (size_t)-1;
#endif
▾Minclude/cutils/dyn_string.h
@@ -30,8 +30,10 @@ void string_remove_range(String* string, size_t index, size_t length);
bool_t string_adjust_size(String* string, size_t size);
size_t* string_find_char(const String* haystack, const char needle);
size_t* string_find_str(const String* haystack, const String* needle);
bool_t string_concat(String* string, const String* other);
String* from_cstring(const char* cstring);
String* from_cstring_del(char* cstring);
char* to_cstring(const String* string);
▾Ainclude/cutils/hedley.h
@@ -0,0 +1,704 @@
/* Hedley - https://nemequ.github.io/hedley
* Created by Evan Nemerson <evan@nemerson.com>
*
* To the extent possible under law, the author(s) have dedicated all
* copyright and related and neighboring rights to this software to
* the public domain worldwide. This software is distributed without
* any warranty.
*
* For details, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#if !defined(HEDLEY_VERSION) || (HEDLEY_VERSION < 3)
#if defined(HEDLEY_VERSION)
# undef HEDLEY_VERSION
#endif
#define HEDLEY_VERSION 3
#if defined(HEDLEY_VERSION_ENCODE)
# undef HEDLEY_VERSION_ENCODE
#endif
#define HEDLEY_VERSION_ENCODE(major,minor,revision) (((major) * 1000000) + ((minor) * 1000) + (revision))
#if defined(HEDLEY_VERSION_DECODE_MAJOR)
# undef HEDLEY_VERSION_DECODE_MAJOR
#endif
#define HEDLEY_VERSION_DECODE_MAJOR(version) ((version) / 1000000)
#if defined(HEDLEY_VERSION_DECODE_MINOR)
# undef HEDLEY_VERSION_DECODE_MINOR
#endif
#define HEDLEY_VERSION_DECODE_MINOR(version) (((version) % 1000000) / 1000)
#if defined(HEDLEY_VERSION_DECODE_REVISION)
# undef HEDLEY_VERSION_DECODE_REVISION
#endif
#define HEDLEY_VERSION_DECODE_REVISION(version) ((version) % 1000)
#if defined(HEDLEY_GCC_VERSION_CHECK)
# undef HEDLEY_GCC_VERSION_CHECK
#endif
#if !defined(__GNUC__) || defined(__INTEL_COMPILER)
# define HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (0)
#else
# if defined(__GNUC_PATCHLEVEL__)
# define HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) >= HEDLEY_VERSION_ENCODE(major,minor,patch))
# else
# define HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, 0) >= HEDLEY_VERSION_ENCODE(major,minor,patch))
# endif
#endif
#if defined(HEDLEY_GCC_NOT_CLANG_VERSION_CHECK)
# undef HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
#endif
#if defined(__clang__)
# define HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) (0)
#else
# define HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
#endif
#if defined(HEDLEY_MSVC_VERSION_CHECK)
# undef HEDLEY_GCC_VERSION_CHECK
#endif
#if !defined(_MSC_VER)
# define HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (0)
#elif defined(_MSC_VER) && (_MSC_VER >= 1400)
# define HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 1000000) + (minor * 10000) + (patch)))
#elif defined(_MSC_VER) && (_MSC_VER >= 1200)
# define HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 100000) + (minor * 1000) + (patch)))
#else
# define HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_VER >= ((major * 100) + (minor)))
#endif
#if defined(HEDLEY_INTEL_VERSION_CHECK)
# undef HEDLEY_INTEL_VERSION_CHECK
#endif
#if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE)
# define HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) ((__INTEL_COMPILER + __INTEL_COMPILER_UPDATE) >= (((major) * 100) + (minor)))
#elif defined(__INTEL_COMPILER)
# define HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (__INTEL_COMPILER >= (((major) * 100) + (minor)))
#else
# define HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (0)
#endif
#if defined(HEDLEY_PGI_VERSION_CHECK)
# undef HEDLEY_PGI_VERSION_CHECK
#endif
#if defined(__PGI) && defined(__PGIC__) && defined(__PGIC_MINOR__) && defined(__PGIC_PATCHLEVEL__)
# define HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (HEDLEY_VERSION_ENCODE(__PGIC__, __PGIC_MINOR__, __PGIC_PATCHLEVEL__) >= HEDLEY_VERSION_ENCODE(major,minor,patch))
#else
# define HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (0)
#endif
#if defined(HEDLEY_SUNPRO_VERSION_CHECK)
# undef HEDLEY_SUNPRO_VERSION_CHECK
#endif
#if defined(__SUNPRO_C)
# define HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) \
(__SUNPRO_C >= ((((major) > 5) || (((major) == 5) && ((minor) > 9))) ? \
(((major) * 0x1000) + (((minor + (6 * (minor / 10)))) * 0x10) + (revision)) : \
(((major) * 0x100) + ((minor) * 0x10) + (revision))))
#elif defined(__SUNPRO_CC)
# define HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) \
(__SUNPRO_CC >= ((((major) > 5) || (((major) == 5) && ((minor) > 9))) ? \
(((major) * 0x1000) + (((minor + (6 * (minor / 10)))) * 0x10) + (revision)) : \
(((major) * 0x100) + ((minor) * 0x10) + (revision))))
#else
# define HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (0)
#endif
#if defined(HEDLEY_EMSCRIPTEN_VERSION_CHECK)
# undef HEDLEY_EMSCRIPTEN_VERSION_CHECK
#endif
#if defined(__EMSCRIPTEN__)
# define HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) \
(HEDLEY_VERSION_ENCODE(__EMSCRIPTEN_major__, __EMSCRIPTEN_minor__, __EMSCRIPTEN_tiny__) >= HEDLEY_VERSION_ENCODE(major, minor, patch))
#else
# define HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (0)
#endif
#if defined(HEDLEY_ARM_VERSION_CHECK)
# undef HEDLEY_ARM_VERSION_CHECK
#endif
#if defined(__ARMCOMPILER_VERSION)
# define HEDLEY_ARM_VERSION_CHECK(major,minor,patch) ((__ARMCOMPILER_VERSION) >= ((major * 1000000) + (minor * 10000) + (patch * 100)))
#elif defined(__CC_ARM) && defined(__ARMCC_VERSION)
# define HEDLEY_ARM_VERSION_CHECK(major,minor,patch) ((__ARMCC_VERSION) >= ((major * 1000000) + (minor * 10000) + (patch)))
#else
# define HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (0)
#endif
#if defined(HEDLEY_IBM_VERSION_CHECK)
# undef HEDLEY_IBM_VERSION_CHECK
#endif
#if defined(__IBMC__)
# define HEDLEY_IBM_VERSION_CHECK(major,minor,patch) ((((major) * 100) + ((minor) * 10) + (patch)) >= (__IBMC__))
#elif defined(__IBMCPP__)
# define HEDLEY_IBM_VERSION_CHECK(major,minor,patch) ((((major) * 100) + ((minor) * 10) + (patch)) >= (__IBMCPP__))
#else
# define HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (0)
#endif
#if \
(defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
(HEDLEY_IBM_VERSION_CHECK(10,1,0) && defined(__C99_PRAGMA_OPERATOR))
# define HEDLEY_PRAGMA(value) _Pragma(#value)
#elif HEDLEY_MSVC_VERSION_CHECK(15,0,0)
# define HEDLEY_PRAGMA(value) __pragma(value)
#else
# define HEDLEY_PRAGMA(value)
#endif
#if defined(HEDLEY_CLANG_HAS_ATTRIBUTE)
# undef HEDLEY_CLANG_HAS_ATTRIBUTE
#endif
#if defined(HEDLEY_CLANG_HAS_CPP_ATTRIBUTE)
# undef HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
#endif
#if defined(HEDLEY_CLANG_HAS_BUILTIN)
# undef HEDLEY_CLANG_HAS_BUILTIN
#endif
#if defined(HEDLEY_CLANG_HAS_FEATURE)
# undef HEDLEY_CLANG_HAS_FEATURE
#endif
#if defined(HEDLEY_CLANG_HAS_EXTENSION)
# undef HEDLEY_CLANG_HAS_EXTENSION
#endif
#if defined(HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE)
# undef HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE
#endif
#if defined(HEDLEY_CLANG_HAS_WARNING)
# undef HEDLEY_CLANG_HAS_WARNING
#endif
#if defined(__has_attribute)
# define HEDLEY_CLANG_HAS_ATTRIBUTE(attribute) __has_attribute(attribute)
#else
# define HEDLEY_CLANG_HAS_ATTRIBUTE(attribute) (0)
#endif
#if defined(__has_cpp_attribute) && defined(__cplusplus)
# define HEDLEY_CLANG_HAS_CPP_ATTRIBUTE(attribute) __has_cpp_attribute(attribute)
#else
# define HEDLEY_CLANG_HAS_CPP_ATTRIBUTE(attribute) (0)
#endif
#if defined(__has_builtin)
# define HEDLEY_CLANG_HAS_BUILTIN(builtin) __has_builtin(builtin)
#else
# define HEDLEY_CLANG_HAS_BUILTIN(builtin) (0)
#endif
#if defined(__has_feature)
# define HEDLEY_CLANG_HAS_FEATURE(feature) __has_feature(feature)
#else
# define HEDLEY_CLANG_HAS_FEATURE(feature) (0)
#endif
#if defined(__has_extension)
# define HEDLEY_CLANG_HAS_EXTENSION(extension) __has_extension(extension)
#else
# define HEDLEY_CLANG_HAS_EXTENSION(extension) (0)
#endif
#if defined(__has_declspec_attribute)
# define HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE(attribute) __has_declspec_attribute(attribute)
#else
# define HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE(attribute) (0)
#endif
#if defined(__has_warning)
# define HEDLEY_CLANG_HAS_WARNING(warning) __has_warning(warning)
#else
# define HEDLEY_CLANG_HAS_WARNING(warning) (0)
#endif
#if defined(HEDLEY_GCC_HAS_ATTRIBUTE)
# undef HEDLEY_GCC_HAS_ATTRIBUTE
#endif
#if defined(__has_attribute)
# define HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) __has_attribute(attribute)
#else
# define HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
#endif
#if defined(HEDLEY_GCC_HAS_CPP_ATTRIBUTE)
# undef HEDLEY_GCC_HAS_CPP_ATTRIBUTE
#endif
#if defined(__has_cpp_attribute) && defined(__cplusplus)
# define HEDLEY_GCC_HAS_CPP_ATTRIBUTE(cpp_attribute,major,minor,patch) __has_cpp_attribute(cpp_attribute)
#else
# define HEDLEY_GCC_HAS_CPP_ATTRIBUTE(cpp_attribute,major,minor,patch) HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
#endif
#if defined(HEDLEY_GCC_HAS_BUILTIN)
# undef HEDLEY_GCC_HAS_BUILTIN
#endif
#if defined(__has_builtin)
# define HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
#else
# define HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
#endif
#if defined(HEDLEY_GCC_HAS_FEATURE)
# undef HEDLEY_GCC_HAS_FEATURE
#endif
#if defined(__has_feature)
# define HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
#else
# define HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
#endif
#if defined(HEDLEY_GCC_HAS_EXTENSION)
# undef HEDLEY_GCC_HAS_EXTENSION
#endif
#if defined(__has_extension)
# define HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
#else
# define HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
#endif
#if defined(HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE)
# undef HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
#endif
#if defined(__has_declspec_attribute)
# define HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(declspec_attribute,major,minor,patch) __has_declspec_attribute(declspec_attribute)
#else
# define HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(declspec_attribute,major,minor,patch) HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
#endif
#if defined(HEDLEY_GCC_HAS_WARNING)
# undef HEDLEY_GCC_HAS_WARNING
#endif
#if defined(__has_warning)
# define HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
#else
# define HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
#endif
#if defined(HEDLEY_DIAGNOSTIC_PUSH)
# undef HEDLEY_DIAGNOSTIC_PUSH
#endif
#if defined(HEDLEY_DIAGNOSTIC_POP)
# undef HEDLEY_DIAGNOSTIC_POP
#endif
#if defined(__clang__)
# define HEDLEY_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push")
# define HEDLEY_DIAGNOSTIC_POP _Pragma("clang diagnostic pop")
#elif HEDLEY_INTEL_VERSION_CHECK(16,0,0)
# define HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
# define HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
#elif HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(4,6,0)
# define HEDLEY_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
# define HEDLEY_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
#elif HEDLEY_MSVC_VERSION_CHECK(15,0,0)
# define HEDLEY_DIAGNOSTIC_PUSH __pragma(warning(push))
# define HEDLEY_DIAGNOSTIC_POP __pragma(warning(pop))
#else
# define HEDLEY_DIAGNOSTIC_PUSH
# define HEDLEY_DIAGNOSTIC_POP
#endif
#if defined(HEDLEY_DEPRECATED)
# undef HEDLEY_DEPRECATED
#endif
#if defined(HEDLEY_DEPRECATED_FOR)
# undef HEDLEY_DEPRECATED_FOR
#endif
#if defined(__cplusplus) && (__cplusplus >= 201402L)
# define HEDLEY_DEPRECATED(since) [[deprecated("Since " #since)]]
# define HEDLEY_DEPRECATED_FOR(since, replacement) [[deprecated("Since " #since "; use " #replacement)]]
#elif \
HEDLEY_GCC_HAS_EXTENSION(attribute_deprecated_with_message,4,5,0) || \
HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
HEDLEY_ARM_VERSION_CHECK(5,6,0)
# define HEDLEY_DEPRECATED(since) __attribute__((__deprecated__("Since " #since)))
# define HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__("Since " #since "; use " #replacement)))
#elif \
HEDLEY_GCC_HAS_ATTRIBUTE(deprcated,4,0,0) || \
HEDLEY_ARM_VERSION_CHECK(4,1,0)
# define HEDLEY_DEPRECATED(since) __attribute__((__deprecated__))
# define HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__))
#elif HEDLEY_MSVC_VERSION_CHECK(14,0,0)
# define HEDLEY_DEPRECATED(since) __declspec(deprecated("Since " # since))
# define HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated("Since " #since "; use " #replacement))
#elif HEDLEY_MSVC_VERSION_CHECK(13,10,0)
# define HEDLEY_DEPRECATED(since) _declspec(deprecated)
# define HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated)
#else
# define HEDLEY_DEPRECATED(since)
# define HEDLEY_DEPRECATED_FOR(since, replacement)
#endif
#if defined(HEDLEY_UNAVAILABLE)
# undef HEDLEY_UNAVAILABLE
#endif
#if \
HEDLEY_GCC_HAS_ATTRIBUTE(warning,4,3,0) || \
HEDLEY_INTEL_VERSION_CHECK(16,0,0)
# define HEDLEY_UNAVAILABLE(available_since) __attribute__((__warning__("Not available until " #available_since)))
#else
# define HEDLEY_UNAVAILABLE(available_since)
#endif
#if defined(HEDLEY_WARN_UNUSED_RESULT)
# undef HEDLEY_WARN_UNUSED_RESULT
#endif
#if defined(__cplusplus) && (__cplusplus >= 201703L)
# define HEDLEY_WARN_UNUSED_RESULT [[nodiscard]]
#elif HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)
# define HEDLEY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
#elif \
HEDLEY_GCC_HAS_ATTRIBUTE(warn_unused_result,3,4,0) || \
HEDLEY_INTEL_VERSION_CHECK(16,0,0)
# define HEDLEY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
#elif defined(_Check_return_) /* SAL */
# define HEDLEY_WARN_UNUSED_RESULT _Check_return_
#else
# define HEDLEY_WARN_UNUSED_RESULT
#endif
#if defined(HEDLEY_SENTINEL)
# undef HEDLEY_SENTINEL
#endif
#if \
HEDLEY_GCC_HAS_ATTRIBUTE(sentinel,4,0,0) || \
HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
HEDLEY_ARM_VERSION_CHECK(5,4,0)
# define HEDLEY_SENTINEL(position) __attribute__((__sentinel__(position)))
#else
# define HEDLEY_SENTINEL(position)
#endif
#if defined(HEDLEY_NO_RETURN)
# undef HEDLEY_NO_RETURN
#endif
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
# define HEDLEY_NO_RETURN _Noreturn
#elif defined(__cplusplus) && (__cplusplus >= 201103L)
# define HEDLEY_NO_RETURN [[noreturn]]
#elif \
HEDLEY_GCC_HAS_ATTRIBUTE(noreturn,3,2,0) || \
HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
HEDLEY_IBM_VERSION_CHECK(10,1,0)
# define HEDLEY_NO_RETURN __attribute__((__noreturn__))
#elif HEDLEY_MSVC_VERSION_CHECK(13,10,0)
# define HEDLEY_NO_RETURN __declspec(noreturn)
#else
# define HEDLEY_NO_RETURN
#endif
#if defined(HEDLEY_UNREACHABLE)
# undef HEDLEY_UNREACHABLE
#endif
#if (HEDLEY_GCC_HAS_BUILTIN(__builtin_unreachable,4,5,0) && !defined(__CC_ARM)) || HEDLEY_INTEL_VERSION_CHECK(16,0,0)
# define HEDLEY_UNREACHABLE() __builtin_unreachable()
#elif HEDLEY_MSVC_VERSION_CHECK(13,10,0)
# define HEDLEY_UNREACHABLE() __assume(0)
#elif defined(EXIT_FAILURE)
# define HEDLEY_UNREACHABLE() abort()
#else
# define HEDLEY_UNREACHABLE()
#endif
HEDLEY_DIAGNOSTIC_PUSH
#if HEDLEY_GCC_HAS_WARNING("-Wvariadic-macros",4,0,0)
# if defined(__clang__)
# pragma clang diagnostic ignored "-Wvariadic-macros"
# elif defined(__GNUC__)
# pragma GCC diagnostic ignored "-Wvariadic-macros"
# endif
#endif
#if defined(HEDLEY_NON_NULL)
# undef HEDLEY_NON_NULL
#endif
#if HEDLEY_GCC_HAS_ATTRIBUTE(nonnull,3,3,0) || HEDLEY_INTEL_VERSION_CHECK(16,0,0) || HEDLEY_ARM_VERSION_CHECK(4,1,0)
# define HEDLEY_NON_NULL(...) __attribute__((__nonnull__(__VA_ARGS__)))
#else
# define HEDLEY_NON_NULL(...)
#endif
HEDLEY_DIAGNOSTIC_POP
#if defined(HEDLEY_PRINTF_FORMAT)
# undef HEDLEY_PRINTF_FORMAT
#endif
#if defined(__MINGW32__) && HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && !defined(__USE_MINGW_ANSI_STDIO)
# define HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(ms_printf, string_idx, first_to_check)))
#elif defined(__MINGW32__) && HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && defined(__USE_MINGW_ANSI_STDIO)
# define HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(gnu_printf, string_idx, first_to_check)))
#elif \
HEDLEY_GCC_HAS_ATTRIBUTE(format,3,1,0) || \
HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
HEDLEY_IBM_VERSION_CHECK(10,1,0)
# define HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(__printf__, string_idx, first_to_check)))
#else
# define HEDLEY_PRINTF_FORMAT(string_idx,first_to_check)
#endif
#if defined(HEDLEY_LIKELY)
# undef HEDLEY_LIKELY
#endif
#if defined(HEDLEY_UNLIKELY)
# undef HEDLEY_UNLIKELY
#endif
#if \
HEDLEY_GCC_HAS_BUILTIN(__builtin_expect,3,0,0) || \
HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
HEDLEY_SUNPRO_VERSION_CHECK(5,12,0) || \
HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
HEDLEY_IBM_VERSION_CHECK(10,1,0)
# define HEDLEY_LIKELY(expr) __builtin_expect(!!(expr), 1)
# define HEDLEY_UNLIKELY(expr) __builtin_expect(!!(expr), 0)
#else
# define HEDLEY_LIKELY(expr) (!!(expr))
# define HEDLEY_UNLIKELY(expr) (!!(expr))
#endif
#if defined(HEDLEY_MALLOC)
# undef HEDLEY_MALLOC
#endif
#if \
HEDLEY_GCC_HAS_ATTRIBUTE(malloc, 3, 1, 0) || \
HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
HEDLEY_IBM_VERSION_CHECK(12,1,0)
# define HEDLEY_MALLOC __attribute__((__malloc__))
#elif HEDLEY_MSVC_VERSION_CHECK(14, 0, 0)
# define HEDLEY_MALLOC __declspec(restrict)
#else
# define HEDLEY_MALLOC
#endif
#if defined(HEDLEY_PURE)
# undef HEDLEY_PURE
#endif
#if \
HEDLEY_GCC_HAS_ATTRIBUTE(pure, 2, 96, 0) || \
HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
HEDLEY_IBM_VERSION_CHECK(10,1,0)
# define HEDLEY_PURE __attribute__((__pure__))
#elif HEDLEY_MSVC_VERSION_CHECK(14, 0, 0)
# define HEDLEY_PURE __declspec(noalias)
#else
# define HEDLEY_PURE
#endif
#if defined(HEDLEY_CONST)
# undef HEDLEY_CONST
#endif
#if HEDLEY_GCC_HAS_ATTRIBUTE(const, 2, 5, 0) || \
HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
HEDLEY_IBM_VERSION_CHECK(10,1,0)
# define HEDLEY_CONST __attribute__((__const__))
#else
# define HEDLEY_CONST HEDLEY_PURE
#endif
#if defined(HEDLEY_RESTRICT)
# undef HEDLEY_RESTRICT
#endif
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
# define HEDLEY_RESTRICT restrict
#elif \
HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
HEDLEY_PGI_VERSION_CHECK(17,10,0)
# define HEDLEY_RESTRICT __restrict
#elif HEDLEY_SUNPRO_VERSION_CHECK(5,8,0)
# define HEDLEY_RESTRICT _Restrict
#endif
#if defined(HEDLEY_INLINE)
# undef HEDLEY_INLINE
#endif
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
# define HEDLEY_INLINE inline
#elif defined(__GNUC_STDC_INLINE__) || HEDLEY_ARM_VERSION_CHECK(6,2,0)
# define HEDLEY_INLINE __inline__
#elif HEDLEY_MSVC_VERSION_CHECK(12,0,0) || HEDLEY_ARM_VERSION_CHECK(4,1,0)
# define HEDLEY_INLINE __inline
#else
# define HEDLEY_INLINE
#endif
#if defined(HEDLEY_ALWAYS_INLINE)
# undef HEDLEY_ALWAYS_INLINE
#endif
#if \
HEDLEY_GCC_HAS_ATTRIBUTE(always_inline,4,0,0) || \
HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
HEDLEY_IBM_VERSION_CHECK(10,1,0)
# define HEDLEY_ALWAYS_INLINE __attribute__((__always_inline__))
#elif HEDLEY_MSVC_VERSION_CHECK(12,0,0)
# define HEDLEY_ALWAYS_INLINE __forceinline
#else
# define HEDLEY_ALWAYS_INLINE HEDLEY_INLINE
#endif
#if defined(HEDLEY_NEVER_INLINE)
# undef HEDLEY_NEVER_INLINE
#endif
#if \
HEDLEY_GCC_HAS_ATTRIBUTE(never_inline,4,0,0) || \
HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
HEDLEY_IBM_VERSION_CHECK(10,1,0)
# define HEDLEY_NEVER_INLINE __attribute__((__noinline__))
#elif HEDLEY_MSVC_VERSION_CHECK(13,10,0)
# define HEDLEY_NEVER_INLINE __declspec(noinline)
#else
# define HEDLEY_NEVER_INLINE HEDLEY_INLINE
#endif
#if defined(HEDLEY_PRIVATE)
# undef HEDLEY_PRIVATE
#endif
#if defined(HEDLEY_PUBLIC)
# undef HEDLEY_PUBLIC
#endif
#if defined(HEDLEY_IMPORT)
# undef HEDLEY_IMPORT
#endif
#if defined(_WIN32) || defined(__CYGWIN__)
# if \
HEDLEY_GCC_VERSION_CHECK(4,2,0) || \
HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
HEDLEY_IBM_VERSION_CHECK(13,1,0)
# define HEDLEY_PRIVATE __attribute__((__visibility__("hidden")))
# else
# define HEDLEY_PRIVATE
# endif
# define HEDLEY_PUBLIC __declspec(dllexport)
# define HEDLEY_IMPORT __declspec(dllimport)
#else
# if \
HEDLEY_GCC_VERSION_CHECK(4,2,0) || \
HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
HEDLEY_IBM_VERSION_CHECK(13,1,0)
# define HEDLEY_PRIVATE __attribute__((__visibility__("hidden")))
# define HEDLEY_PUBLIC __attribute__((__visibility__("default")))
# else
# define HEDLEY_PRIVATE
# define HEDLEY_PUBLIC
# endif
# define HEDLEY_IMPORT extern
#endif
#if defined(HEDLEY_NO_THROW)
# undef HEDLEY_NO_THROW
#endif
#if \
HEDLEY_GCC_HAS_ATTRIBUTE(nothrow,3,3,0) || \
HEDLEY_INTEL_VERSION_CHECK(16,0,0)
# define HEDLEY_NO_THROW __attribute__((__nothrow__))
#elif \
HEDLEY_MSVC_VERSION_CHECK(13,1,0) || \
HEDLEY_ARM_VERSION_CHECK(4,1,0)
# define HEDLEY_NO_THROW __declspec(nothrow)
#else
# define HEDLEY_NO_THROW
#endif
#if defined(HEDLEY_FALL_THROUGH)
# undef HEDLEY_FALL_THROUGH
#endif
#if defined(__cplusplus) && (__cplusplus >= 201703L)
# define HEDLEY_FALL_THROUGH [[fallthrough]]
#elif defined(__cplusplus) && (__cplusplus >= 201103L) && HEDLEY_CLANG_HAS_CPP_ATTRIBUTE(clang::fallthrough)
# define HEDLEY_FALL_THROUGH [[clang::fallthrough]]
#elif defined(__cplusplus) && (__cplusplus >= 201103L) && HEDLEY_GCC_HAS_CPP_ATTRIBUTE(gnu::fallthrough,7,0,0)
# define HEDLEY_FALL_THROUGH [[gnu::fallthrough]]
#elif HEDLEY_GCC_HAS_ATTRIBUTE(fallthrough,7,0,0)
# define HEDLEY_FALL_THROUGH __attribute__((__fallthrough__))
#elif defined(__fallthrough) /* SAL */
# define HEDLEY_FALL_THROUGH __fallthrough
#else
# define HEDLEY_FALL_THROUGH
#endif
#if defined(HEDLEY_RETURNS_NON_NULL)
# undef HEDLEY_RETURNS_NON_NULL
#endif
#if \
HEDLEY_GCC_HAS_ATTRIBUTE(returns_nonnull,4,9,0) || \
HEDLEY_INTEL_VERSION_CHECK(16,0,0)
# define HEDLEY_RETURNS_NON_NULL __attribute__((__returns_nonnull__))
#elif defined(_Ret_notnull_) /* SAL */
# define HEDLEY_RETURNS_NON_NULL _Ret_notnull_
#else
# define HEDLEY_RETURNS_NON_NULL
#endif
#if defined(HEDLEY_ARRAY_PARAM)
# undef HEDLEY_ARRAY_PARAM
#endif
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && !defined(__cplusplus) && !defined(__PGI)
# define HEDLEY_ARRAY_PARAM(name) (name)
#else
# define HEDLEY_ARRAY_PARAM(name)
#endif
#if defined(HEDLEY_BEGIN_C_DECLS)
# undef HEDLEY_BEGIN_C_DECLS
#endif
#if defined(HEDLEY_END_C_DECLS)
# undef HEDLEY_END_C_DECLS
#endif
#if defined(HEDLEY_C_DECL)
# undef HEDLEY_C_DECL
#endif
#if defined(__cplusplus)
# define HEDLEY_BEGIN_C_DECLS extern "C" {
# define HEDLEY_END_C_DECLS }
# define HEDLEY_C_DECL extern "C"
#else
# define HEDLEY_BEGIN_C_DECLS
# define HEDLEY_END_C_DECLS
# define HEDLEY_C_DECL
#endif
#if defined(HEDLEY_STATIC_ASSERT)
# undef HEDLEY_STATIC_ASSERT
#endif
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
# define HEDLEY_STATIC_ASSERT(expr, message) _Static_assert(expr, message)
#elif defined(__cplusplus) && __cplusplus >= 201103L
# define HEDLEY_STATIC_ASSERT(expr, message) static_assert(expr, message)
#elif \
HEDLEY_GCC_HAS_FEATURE(c_static_assert,4,6,0) || \
HEDLEY_INTEL_VERSION_CHECK(16,0,0)
# define HEDLEY_STATIC_ASSERT(expr, message) _Static_assert(expr, message)
#elif HEDLEY_MSVC_VERSION_CHECK(16,0,0)
# define HEDLEY_STATIC_ASSERT(expr, message) static_assert(expr, message)
#else
# define HEDLEY_STATIC_ASSERT(expr, message)
#endif
#if defined(HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED)
# undef HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
#endif
#if HEDLEY_CLANG_HAS_WARNING("-Wdeprecated")
# define HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("clang diagnostic ignored \"-Wdeprecated\"")
#elif HEDLEY_INTEL_VERSION_CHECK(16,0,0)
# define HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warning(disable:1478)")
#elif HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(4,3,0)
# define HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("GCC diagnostic ignored \"-Wdeprecated\"")
#elif HEDLEY_MSVC_VERSION_CHECK(15,0,0)
# define HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:4996))
#else
# define HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
#endif
#endif /* !defined(HEDLEY_VERSION) || (HEDLEY_VERSION < X) */
▾Minclude/cutils/linked-list.h
@@ -1,6 +1,9 @@
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
#include <cutils/common.h>
#include <stdlib.h>
typedef struct SLnode
{
void* item;
@@ -16,26 +19,102 @@ typedef struct DLnode
typedef struct LinkedList
{
void* head;
void* head; /*SLnode if doubly_linked is false, otherwise DLnode*/
void* tail;
size_t length;
bool_t doubly_linked;
bool_t circularly_linked;
} LinkedList;
LinkedList* new_ll(bool_t doubly_linked);
void delete_ll(LinkedList* ll, void(*rmv) (void*));
LinkedList* new_ll(bool_t doubly_linked, bool_t circularly_linked);
void delete_sll(LinkedList* ll, void(*rmv) (void*));
void delete_dll(LinkedList* ll, void(*rmv) (void*));
HEDLEY_INLINE
static void delete_ll(LinkedList* ll, void(*rmv) (void*))
{
if(ll->doubly_linked)
delete_dll(ll, rmv);
else
delete_sll(ll, rmv);
}
void* ll_at(const LinkedList* ll, size_t index);
void* sll_at(const LinkedList* ll, size_t index);
void* dll_at(const LinkedList* ll, size_t index);
HEDLEY_INLINE
static void* ll_at(const LinkedList* ll, size_t index)
{
if(ll->doubly_linked)
return sll_at(ll, index);
else
return dll_at(ll, index);
}
#define ll_pop(ll) ll_pop_at(ll, ll->length-1)
void* ll_pop_at(LinkedList* ll, size_t index);
void* sll_pop_at(LinkedList* ll, size_t index);
void* dll_pop_at(LinkedList* ll, size_t index);
HEDLEY_INLINE
static void* ll_pop_at(LinkedList* ll, size_t index)
{
if(ll->doubly_linked)
return sll_pop_at(ll, index);
else
return dll_pop_at(ll, index);
}
bool_t ll_insert(LinkedList* ll, size_t index, void* item);
#define ll_push(ll, item) ll_insert(ll, ll->length, item)
bool_t sll_insert(LinkedList* ll, size_t index, void* item);
bool_t dll_insert(LinkedList* ll, size_t index, void* item);
HEDLEY_INLINE
static bool_t ll_insert(LinkedList* ll, size_t index, void* item)
{
if(ll->doubly_linked)
return sll_insert(ll, index, item);
else
return dll_insert(ll, index, item);
}
void ll_remove(LinkedList* ll, size_t index, void (*rmv)(void*));
void ll_remove_range(LinkedList* ll, size_t index, size_t length, void (*rmv)(void*));
size_t* ll_find(const LinkedList* haystack, const void* needle, int (*cmp)(const void*, const void*));
void sll_remove(LinkedList* ll, size_t index, void (*rmv)(void*));
void dll_remove(LinkedList* ll, size_t index, void (*rmv)(void*));
HEDLEY_INLINE
static void ll_remove(LinkedList* ll, size_t index, void (*rmv)(void*))
{
if(ll->doubly_linked)
sll_remove(ll, index, rmv);
else
dll_remove(ll, index, rmv);
}
void sll_remove_range(LinkedList* ll, size_t index, size_t length, void (*rmv)(void*));
void dll_remove_range(LinkedList* ll, size_t index, size_t length, void (*rmv)(void*));
HEDLEY_INLINE
static void ll_remove_range(LinkedList* ll, size_t index, size_t length, void (*rmv)(void*))
{
if(ll->doubly_linked)
sll_remove_range(ll, index, length, rmv);
else
dll_remove_range(ll, index, length, rmv);
}
size_t* sll_find(const LinkedList* haystack, const void* needle, int (*cmp)(const void*, const void*));
size_t* dll_find(const LinkedList* haystack, const void* needle, int (*cmp)(const void*, const void*));
HEDLEY_INLINE
static size_t* ll_find(const LinkedList* haystack, const void* needle, int (*cmp)(const void*, const void*))
{
if(haystack->doubly_linked)
return sll_find(haystack, needle, cmp);
else
return dll_find(haystack, needle, cmp);
}
void sll_swap(LinkedList* ll, size_t item1, size_t item2);
void dll_swap(LinkedList* ll, size_t item1, size_t item2);
HEDLEY_INLINE
static void ll_swap(LinkedList* ll, size_t item1, size_t item2)
{
if(ll->doubly_linked)
sll_swap(ll, item1, item2);
else
dll_swap(ll, item1, item2);
}
#endif /* LINKED_LIST_H */
▾Minclude/cutils/misc.h
@@ -15,15 +15,34 @@
#endif
#include <string.h>
#include <cutils/common.h>
#if __STDC_VERSION__ >= 199901L
#include <stdint.h>
typedef uintmax_t max_uint_t;
uint32_t ntoh32(uint32_t const net);
#ifdef UINT32_MAX
uint32_t ntoh32(uint32_t const net);
#endif
#else
typedef unsigned long max_uint_t;
#endif
HEDLEY_INLINE
static void memswap(void* item1, void* item2, size_t length)
{
char *item1_tmp, *item2_tmp;
char tmp;
item1_tmp = item1;
item2_tmp = item2;
while(length--)
{
tmp = *item1_tmp;
*item1_tmp = *item2_tmp;
*item2_tmp = tmp;
}
}
#if __STDC_VERSION__ >= 201112L
#include <time.h>
▾Minclude/cutils/vector.h
@@ -30,6 +30,7 @@ void vector_remove_range(Vector* vector, size_t index, size_t length, void (*rmv
bool_t vector_adjust_size(Vector* vector, size_t size);
bool_t vector_shrink(Vector* vector);
HEDLEY_NON_NULL(3)
size_t* vector_find(const Vector* haystack, const void* needle, int (*cmp)(const void*, const void*));
▾Mmeson.build
@@ -1,6 +1,6 @@
project('cutils', 'c')
WARNINGS = ['-Wall', '-Wpedantic', '-Wextra', '-Wnull-dereference', '-Wshadow', '-Wconversion', '-Wstrict-prototypes', '-Wmissing-prototypes', '-Wcast-qual', '-Wstrict-overflow=5', '-Wunreachable-code', '-Wno-unused-parameter']
WARNINGS = ['-Wall', '-Wpedantic', '-Wextra', '-Wnull-dereference', '-Wshadow', '-Wconversion', '-Wstrict-prototypes', '-Wmissing-prototypes', '-Wcast-qual', '-Wstrict-overflow=5', '-Wunreachable-code', '-Wno-unused-parameter', '-Wno-unused-function']
CFLAGS = ['-std=c89', '-fstrict-aliasing', '-fPIC'] + WARNINGS
subdir('include')
▾Msrc/byte_array.c
@@ -73,7 +73,7 @@ bool_t bytearray_insert(Bytearray* bytearray, size_t index, const void* item)
memmove(bytearray->items+index*size+1*size, bytearray->items+index*size, (length*size-index*size)*sizeof(*bytearray->items));
memcpy(bytearray->items+index*size, item, bytearray->element_size);
++bytearray->length;
bytearray->length++;
return b_true;
}
@@ -87,7 +87,7 @@ void bytearray_remove(Bytearray* bytearray, size_t index, void (*rmv)(void*))
rmv(&bytearray->items[index*elsize]);
memmove(bytearray->items+index*elsize, bytearray->items+index*elsize+1*elsize, (length*elsize-index*elsize-1*elsize)*sizeof(*bytearray->items));
--bytearray->length;
bytearray->length--;
}
}
▾Msrc/dyn_string.c
@@ -22,7 +22,7 @@ void string_remove(String* string, size_t index)
if(index < string->length)
{
memmove(string->chars+index, string->chars+index+1, (string->length-index-1)*sizeof(*string->chars));
--string->length;
string->length--;
}
}
@@ -77,8 +77,8 @@ bool_t string_concat(String* string, const String* other)
size_t* string_find_char(const String* haystack, const char needle)
{
size_t* ret;
size_t i;
size_t i, *ret;
for(i = 0; i < haystack->length; i++)
{
if(haystack->chars[i] == needle)
@@ -91,6 +91,27 @@ size_t* string_find_char(const String* haystack, const char needle)
return NULL;
}
size_t* string_find_str(const String* haystack, const String* needle)
{
size_t *ret, i, j, tmp;
for(i = 0; i < haystack->length; i++)
{
for(j = 0, tmp = i; j < needle->length; j++, i++)
{
if(haystack->chars[i] != needle->chars[j])
{
break;
} else if(j == needle->length-1){
ret = malloc(sizeof(*ret));
*ret = tmp;
return ret;
}
}
}
return NULL;
}
bool_t string_adjust_size(String* string, size_t size)
{
while(string->capacity < size)
▾Asrc/linked-list.c
@@ -0,0 +1,373 @@
#include <cutils/linked-list.h>
#include <cutils/misc.h>
LinkedList* new_ll(bool_t doubly_linked, bool_t circularly_linked)
{
LinkedList* ret = malloc(sizeof(*ret));
ret->head = NULL;
ret->tail = NULL;
ret->length = 0;
ret->doubly_linked = doubly_linked;
ret->circularly_linked = circularly_linked;
return ret;
}
void delete_sll(LinkedList* ll, void(*rmv) (void*))
{
size_t i;
SLnode *current, *tmp;
current = ll->head;
for(i = 0; i < ll->length; i++)
{
if(rmv)
rmv(current->item);
tmp = current;
current = current->next;
free(tmp);
}
}
void delete_dll(LinkedList* ll, void(*rmv) (void*))
{
size_t i;
DLnode *current, *tmp;
current = ll->head;
for(i = 0; i < ll->length; i++)
{
if(rmv)
rmv(current->item);
tmp = current;
current = current->next;
free(tmp);
}
}
void* sll_at(const LinkedList* ll, size_t index)
{
size_t i;
SLnode* current;
current = ll->head;
for(i = 0; i < index; i++)
{
current = current->next;
}
return current->item;
}
void* dll_at(const LinkedList* ll, size_t index)
{
size_t i;
DLnode* current;
current = ll->head;
for(i = 0; i < index; i++)
{
current = current->next;
}
return current->item;
}
void* sll_pop_at(LinkedList* ll, size_t index)
{
size_t i;
SLnode* current;
void* tmp;
current = ll->head;
for(i = 0; i < index; i++)
{
current = current->next;
}
tmp = current->item;
ll_remove(ll, index, NULL);
return tmp;
}
void* dll_pop_at(LinkedList* ll, size_t index)
{
size_t i;
DLnode* current;
void* tmp;
current = ll->head;
for(i = 0; i < index; i++)
{
current = current->next;
}
tmp = current->item;
ll_remove(ll, index, NULL);
return tmp;
}
bool_t sll_insert(LinkedList* ll, size_t index, void* item)
{
size_t i;
SLnode *current, *prev, *newnode = malloc(sizeof(*newnode));
if(!newnode)
return b_false;
newnode->item =item;
prev = NULL;
current = ll->head;
for(i = 0; i < index; i++)
{
prev = current;
current = current->next;
}
if(current)
{
newnode->next = current;
if(prev)
prev->next = newnode;
else
ll->head = newnode;
} else {
ll->tail = newnode;
if(index == 0)
ll->head = newnode;
newnode->next = NULL;
}
ll->length++;
return b_true;
}
bool_t dll_insert(LinkedList* ll, size_t index, void* item)
{
size_t i;
DLnode *current, *newnode = malloc(sizeof(*newnode));
if(!newnode)
return b_false;
newnode->item =item;
current = ll->head;
for(i = 0; i < index; i++)
{
current = current->next;
}
if(current)
{
newnode->next = current;
if(current->prev)
{
current->prev->next = newnode;
newnode->prev =current;
} else {
ll->head = newnode;
newnode->prev = NULL;
}
} else {
ll->tail = newnode;
if(index == 0)
ll->head = newnode;
newnode->next = NULL;
newnode->prev = NULL;
}
ll->length++;
return b_true;
}
void sll_remove(LinkedList* ll, size_t index, void (*rmv)(void*))
{
size_t i;
SLnode *current, *prev;
prev = NULL;
current = ll->head;
for(i = 0; i < index; i++)
{
prev = current;
current = current->next;
}
if(rmv)
rmv(current->item);
prev->next = current->next;
free(current);
}
void dll_remove(LinkedList* ll, size_t index, void (*rmv)(void*))
{
size_t i;
DLnode* current;
current = ll->head;
for(i = 0; i < index; i++)
{
current = current->next;
}
if(rmv)
rmv(current->item);
current->prev->next = current->next;
current->next->prev = current->prev;
free(current);
}
void sll_remove_range(LinkedList* ll, size_t index, size_t length, void (*rmv)(void*))
{
size_t i;
SLnode *current, *tmp, *prev;
prev = NULL;
current = ll->head;
for(i = 0; i < index; i++)
{
prev = current;
current = current->next;
}
for(i = 0; i < length; i++)
{
if(rmv)
rmv(current->item);
tmp = current;
free(current);
current = tmp->next;
}
prev->next = current;
}
void dll_remove_range(LinkedList* ll, size_t index, size_t length, void (*rmv)(void*))
{
size_t i;
DLnode *current, *prev;
prev = NULL;
current = ll->head;
for(i = 0; i < index; i++)
{
prev = current;
current = current->next;
}
for(i = 0; i < length; i++)
{
if(rmv)
rmv(current->item);
current = current->next;
free(current->prev);
}
prev->next = current;
current->prev = prev;
}
size_t* sll_find(const LinkedList* haystack, const void* needle, int (*cmp)(const void*, const void*))
{
size_t i, *ret;
SLnode* current;
current = haystack->head;
for(i = 0; i < haystack->length; i++)
{
if(cmp(current->item, needle) == 0)
{
ret = malloc(sizeof(*ret));
*ret = i;
return ret;
}
current = current->next;
}
return NULL;
}
size_t* dll_find(const LinkedList* haystack, const void* needle, int (*cmp)(const void*, const void*))
{
size_t i, *ret;
DLnode* current;
current = haystack->head;
for(i = 0; i < haystack->length; i++)
{
if(cmp(current->item, needle) == 0)
{
ret = malloc(sizeof(*ret));
*ret = i;
return ret;
}
current = current->next;
}
return NULL;
}
void sll_swap(LinkedList* ll, size_t item1, size_t item2)
{
size_t i, first_index, second_index;
SLnode *first, *second;
first_index = item1 <= item2 ? item1 : item2;
second_index = item1 <= item2 ? item2 : item1;
first = NULL;
second = ll->head;
for(i = 0; i < second_index; i++)
{
if(i == first_index)
first = second->next;
second = second->next;
}
if(!first)
first = second;
memswap(&first->item, &second->item, sizeof(first->item));
}
void dll_swap(LinkedList* ll, size_t item1, size_t item2)
{
size_t i, first_index, second_index;
DLnode *first, *second;
first_index = item1 <= item2 ? item1 : item2;
second_index = item1 <= item2 ? item2 : item1;
first = NULL;
second = ll->head;
for(i = 0; i < second_index; i++)
{
if(i == first_index)
first = second->next;
second = second->next;
}
if(!first)
first = second;
memswap(&first->item, &second->item, sizeof(first->item));
}
▾Msrc/meson.build
@@ -1,4 +1,4 @@
src = ['vector.c', 'dyn_string.c', 'misc.c', 'byte_array.c']
src = ['vector.c', 'dyn_string.c', 'misc.c', 'byte_array.c', 'linked-list.c']
srctest = src + ['test.c']
cutils_lib = library('cutils', src, include_directories : inc, install: true)
▾Msrc/misc.c
@@ -11,6 +11,7 @@ void sleep_ms(unsigned int milliseconds)
}
#if __STDC_VERSION__ >= 199901L
#ifdef UINT32_MAX
uint32_t ntoh32(uint32_t const net)
{
uint8_t data[4];
@@ -22,6 +23,7 @@ uint32_t ntoh32(uint32_t const net)
| ((uint32_t) data[0] << 24);
}
#endif
#endif
#if __STDC_VERSION__ >= 201112L
struct timespec timespec_diff(const struct timespec* old_ts, const struct timespec* new_ts)
▾Msrc/test.c
@@ -70,6 +70,7 @@ static void test2(void)
String* string;
String* string2;
char* cstring;
size_t* tmp;
string = new_string();
@@ -103,6 +104,15 @@ static void test2(void)
delete_string(string2);
delete_string(string);
string = from_cstring("abcd");
string2 = from_cstring("cd");
tmp = string_find_str(string, string2);
assert(*tmp == 2);
free(tmp);
delete_string(string2);
delete_string(string);
}
static void test3(void)
▾Msrc/vector.c
@@ -45,7 +45,7 @@ void vector_remove(Vector* vector, size_t index, void (*rmv)(void*))
if(rmv)
rmv(vector->items[index]);
memmove(vector->items+index, vector->items+index+1, (vector->length-index-1)*sizeof(*vector->items));
--vector->length;
vector->length--;
}
}
@@ -60,8 +60,7 @@ void vector_remove_range(Vector* vector, size_t index, size_t length, void (*rmv
size_t* vector_find(const Vector* haystack, const void* needle, int (*cmp)(const void*, const void*))
{
size_t* ret;
size_t i;
size_t i, *ret;
for(i = 0; i < haystack->length; i++)
{
@@ -82,7 +81,7 @@ bool_t vector_insert(Vector* vector, size_t index, void* item)
memmove(vector->items+index+1, vector->items+index, (vector->length-index)*sizeof(*vector->items));
vector->items[index] = item;
++vector->length;
vector->length++;
return b_true;
}