diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..ab76ec5 --- /dev/null +++ b/.clang-format @@ -0,0 +1,297 @@ +--- +Language: Cpp +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align +AlignArrayOfStructures: None +AlignConsecutiveAssignments: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: false + AlignFunctionPointers: false + PadOperators: true +AlignConsecutiveBitFields: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveDeclarations: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: true + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveMacros: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveShortCaseStatements: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCaseArrows: false + AlignCaseColons: false +AlignConsecutiveTableGenBreakingDAGArgColons: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveTableGenCondOperatorColons: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveTableGenDefinitionColons: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: false + AlignFunctionPointers: false + PadOperators: false +AlignEscapedNewlines: Right +AlignOperands: Align +AlignTrailingComments: + Kind: Always + OverEmptyLines: 0 +AllowAllArgumentsOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: false +AllowBreakBeforeNoexceptSpecifier: Never +AllowShortBlocksOnASingleLine: Never +AllowShortCaseExpressionOnASingleLine: true +AllowShortCaseLabelsOnASingleLine: false +AllowShortCompoundRequirementOnASingleLine: true +AllowShortEnumsOnASingleLine: true +AllowShortFunctionsOnASingleLine: Inline +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: false +AllowShortNamespacesOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AttributeMacros: + - __capability +BinPackArguments: false +BinPackLongBracedList: true +BinPackParameters: OnePerLine +BitFieldColonSpacing: Both +BracedInitializerIndentWidth: -1 +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterExternBlock: false + AfterFunction: true + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: false + SplitEmptyNamespace: true +BreakAdjacentStringLiterals: true +BreakAfterAttributes: Leave +BreakAfterJavaFieldAnnotations: false +BreakAfterReturnType: None +BreakArrays: true +BreakBeforeBinaryOperators: None +BreakBeforeConceptDeclarations: Always +BreakBeforeBraces: Custom +BreakBeforeInlineASMColon: OnlyMultiline +BreakBeforeTemplateCloser: false +BreakBeforeTernaryOperators: true +BreakBinaryOperations: Never +BreakConstructorInitializers: BeforeComma +BreakFunctionDefinitionParameters: false +BreakInheritanceList: BeforeComma +BreakStringLiterals: true +BreakTemplateDeclarations: Yes +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerIndentWidth: 2 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: false +DerivePointerAlignment: false +DisableFormat: false +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: LogicalBlock +EnumTrailingComma: Leave +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: false +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IfMacros: + - KJ_IF_MAYBE +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + SortPriority: 0 + CaseSensitive: false + - Regex: '.*' + Priority: 1 + SortPriority: 0 + CaseSensitive: false +IncludeIsMainRegex: '(Test)?$' +IncludeIsMainSourceRegex: '' +IndentAccessModifiers: false +IndentCaseBlocks: false +IndentCaseLabels: true +IndentExportBlock: true +IndentExternBlock: AfterExternBlock +IndentGotoLabels: true +IndentPPDirectives: None +IndentRequiresClause: true +IndentWidth: 4 +IndentWrappedFunctionNames: false +InsertBraces: false +InsertNewlineAtEOF: false +InsertTrailingCommas: None +IntegerLiteralSeparator: + Binary: 0 + BinaryMinDigits: 0 + Decimal: 0 + DecimalMinDigits: 0 + Hex: 0 + HexMinDigits: 0 +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLines: + AtEndOfFile: false + AtStartOfBlock: true + AtStartOfFile: true +KeepFormFeed: false +LambdaBodyIndentation: Signature +LineEnding: DeriveLF +MacroBlockBegin: '' +MacroBlockEnd: '' +MainIncludeChar: Quote +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: true +ObjCSpaceBeforeProtocolList: false +OneLineFormatOffRegex: '' +PackConstructorInitializers: BinPack +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakBeforeMemberAccess: 150 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakOpenParenthesis: 0 +PenaltyBreakScopeResolution: 500 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyIndentedWhitespace: 0 +PenaltyReturnTypeOnItsOwnLine: 200 +PointerAlignment: Left +PPIndentWidth: -1 +QualifierAlignment: Leave +ReferenceAlignment: Pointer +ReflowComments: Always +RemoveBracesLLVM: false +RemoveEmptyLinesInUnwrappedLines: false +RemoveParentheses: Leave +RemoveSemicolon: false +RequiresClausePosition: OwnLine +RequiresExpressionIndentation: OuterScope +SeparateDefinitionBlocks: Leave +ShortNamespaceLines: 1 +SkipMacroDefinitionBody: false +SortIncludes: + Enabled: true + IgnoreCase: false +SortJavaStaticImport: Before +SortUsingDeclarations: LexicographicNumeric +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterOperatorKeyword: false +SpaceAfterTemplateKeyword: false +SpaceAroundPointerQualifiers: Default +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeJsonColon: false +SpaceBeforeParens: ControlStatements +SpaceBeforeParensOptions: + AfterControlStatements: true + AfterForeachMacros: true + AfterFunctionDefinitionName: false + AfterFunctionDeclarationName: false + AfterIfMacros: true + AfterNot: false + AfterOverloadedOperator: false + AfterPlacementOperator: true + AfterRequiresInClause: false + AfterRequiresInExpression: false + BeforeNonEmptyParentheses: false +SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeSquareBrackets: false +SpaceInEmptyBlock: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: Never +SpacesInContainerLiterals: true +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParens: Never +SpacesInParensOptions: + ExceptDoubleParentheses: false + InCStyleCasts: false + InConditionalStatements: false + InEmptyParentheses: false + Other: false +SpacesInSquareBrackets: false +Standard: Latest +StatementAttributeLikeMacros: + - Q_EMIT +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TableGenBreakInsideDAGArg: DontBreak +TabWidth: 4 +UseTab: Never +VerilogBreakBetweenInstancePorts: true +WhitespaceSensitiveMacros: + - BOOST_PP_STRINGIZE + - CF_SWIFT_NAME + - NS_SWIFT_NAME + - PP_STRINGIZE + - STRINGIZE +WrapNamespaceBodyWithEmptyLines: Leave +... + diff --git a/.github/workflows/c-cpp.yml b/.github/workflows/build-and-sanitize.yml similarity index 100% rename from .github/workflows/c-cpp.yml rename to .github/workflows/build-and-sanitize.yml diff --git a/.github/workflows/check-format.yml b/.github/workflows/check-format.yml new file mode 100644 index 0000000..8e65a63 --- /dev/null +++ b/.github/workflows/check-format.yml @@ -0,0 +1,26 @@ +name: Clang Format Check + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +env: + FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true + +jobs: + format: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Install clang-format + run: | + sudo apt-get update + sudo apt-get install -y clang-format + + - name: Check formatting + run: make format-check diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml deleted file mode 100644 index fe1e5d6..0000000 --- a/.github/workflows/codeql.yml +++ /dev/null @@ -1,50 +0,0 @@ -name: "CodeQL Advanced" - -on: - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] - schedule: - - cron: '28 3 * * 3' - -env: - FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true - -jobs: - analyze: - name: Analyze (${{ matrix.language }}) - runs-on: ubuntu-latest - permissions: - security-events: write - packages: read - contents: read - - strategy: - fail-fast: false - matrix: - include: - - language: c-cpp - build-mode: manual - - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - - name: Initialize CodeQL - uses: github/codeql-action/init@v4 - with: - languages: ${{ matrix.language }} - build-mode: ${{ matrix.build-mode }} - - - name: Run manual build steps - if: matrix.build-mode == 'manual' - shell: bash - run: | - make clean - make all - - - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v4 - with: - category: "/language:${{matrix.language}}" diff --git a/Makefile b/Makefile index ed47a50..c32289b 100644 --- a/Makefile +++ b/Makefile @@ -57,6 +57,12 @@ cppcheck: --suppress=unusedFunction \ --suppress=staticFunction \ $(SRC) $(HEADER) +format: + clang-format -i $(HEADER) $(SRC) + +format-check: + @diff -u <(clang-format dstr.c) dstr.c || echo "Wrong format in dstr.c, run 'make format'" + @diff -u <(clang-format dstr.h) dstr.h || echo "Wrong format in dstr.h, run 'make format'" clean: rm -f *.o *.a *.so $(TEST_EXEC) $(TEST_ASAN_EXEC) diff --git a/dstr.c b/dstr.c index d5f685e..d01e5c6 100644 --- a/dstr.c +++ b/dstr.c @@ -5,9 +5,9 @@ #include "dstr.h" +#include #include #include -#include #include "dstr_options.h" @@ -47,28 +47,31 @@ struct __attribute__((packed)) dstrhd64 { }; // Utils -#define DSTRGETHDR(n,s) \ - ((struct dstrhd##n *)((char*)(s) - sizeof(struct dstrhd##n))) +#define DSTRGETHDR(n, s) \ + ((struct dstrhd##n*)((char*)(s) - sizeof(struct dstrhd##n))) #define DSTRGETTYPE(s) ((uint8_t)((s)[-1])) -static inline enum dstrhd_type _dstr_type_by_size(size_t cap) +static inline enum dstrhd_type _dstr_type_by_size(size_t cap) { - if (cap <= UINT8_MAX) return DSTRHD_TYPE_8; - if (cap <= UINT16_MAX) return DSTRHD_TYPE_16; - if (cap <= UINT32_MAX) return DSTRHD_TYPE_32; + if (cap <= UINT8_MAX) + return DSTRHD_TYPE_8; + if (cap <= UINT16_MAX) + return DSTRHD_TYPE_16; + if (cap <= UINT32_MAX) + return DSTRHD_TYPE_32; return DSTRHD_TYPE_64; } static inline size_t _dstr_size_by_type(enum dstrhd_type type) { - switch(type) { - case DSTRHD_TYPE_8: + switch (type) { + case DSTRHD_TYPE_8: return sizeof(struct dstrhd8); - case DSTRHD_TYPE_16: + case DSTRHD_TYPE_16: return sizeof(struct dstrhd16); - case DSTRHD_TYPE_32: + case DSTRHD_TYPE_32: return sizeof(struct dstrhd32); - case DSTRHD_TYPE_64: + case DSTRHD_TYPE_64: return sizeof(struct dstrhd64); default: return 0; @@ -79,7 +82,7 @@ static inline void _dstr_set_len(void* hd, size_t new_len, enum dstrhd_type type) { - switch(type) { + switch (type) { case DSTRHD_TYPE_8: ((struct dstrhd8*)hd)->len = (uint8_t)new_len; return; @@ -102,7 +105,7 @@ static inline void _dstr_set_cap(void* hd, size_t new_cap, enum dstrhd_type type) { - switch(type) { + switch (type) { case DSTRHD_TYPE_8: ((struct dstrhd8*)hd)->cap = (uint8_t)new_cap; return; @@ -123,7 +126,8 @@ static inline void _dstr_set_cap(void* hd, static inline void* _dstr_get_hdr_and_type(dstr s, enum dstrhd_type* t_out) { - if (!s) return NULL; + if (!s) + return NULL; enum dstrhd_type t = DSTRGETTYPE(s); @@ -151,9 +155,10 @@ static inline void* _dstr_get_hdr_and_type(dstr s, enum dstrhd_type* t_out) // strlen size_t dstrlen(dstr s) { - if(!s) return 0; + if (!s) + return 0; enum dstrhd_type t = DSTRGETTYPE(s); - switch(t) { + switch (t) { case DSTRHD_TYPE_8: { return DSTRGETHDR(8, s)->len; } @@ -173,9 +178,10 @@ size_t dstrlen(dstr s) // dstrcap size_t dstrcap(dstr s) { - if(!s) return 0; + if (!s) + return 0; enum dstrhd_type t = DSTRGETTYPE(s); - switch(t) { + switch (t) { case DSTRHD_TYPE_8: { return DSTRGETHDR(8, s)->cap; } @@ -195,18 +201,21 @@ size_t dstrcap(dstr s) // strdup dstr dstrdup(dstr s) { - if (!s) return NULL; + if (!s) + return NULL; enum dstrhd_type t; const void* old_hd = _dstr_get_hdr_and_type(s, &t); - if (!old_hd) return NULL; + if (!old_hd) + return NULL; size_t len = dstrlen(s); size_t cap = dstrcap(s); size_t hd_size = _dstr_size_by_type(t); void* new_hd = DSTR_MALLOC(hd_size + cap); - if (!new_hd) return NULL; + if (!new_hd) + return NULL; _dstr_set_len(new_hd, len, t); _dstr_set_cap(new_hd, cap, t); @@ -222,7 +231,8 @@ void dstrclear(dstr s) enum dstrhd_type t; void* hd = _dstr_get_hdr_and_type(s, &t); - if (!hd) return; + if (!hd) + return; _dstr_set_len(hd, 0, t); s[0] = '\0'; @@ -234,7 +244,8 @@ void dstrzero(dstr s) enum dstrhd_type t; void* hd = _dstr_get_hdr_and_type(s, &t); - if (!hd) return; + if (!hd) + return; _dstr_set_len(hd, 0, t); @@ -245,7 +256,8 @@ void dstrzero(dstr s) // strreserve dstr dstrreserve(dstr s, size_t new_cap) { - if(!s) return NULL; + if (!s) + return NULL; size_t s_len = dstrlen(s); if (new_cap <= dstrcap(s)) @@ -262,9 +274,9 @@ dstr dstrreserve(dstr s, size_t new_cap) void* old_hd = (char*)s - old_hd_size; - if(old_type == new_type) { + if (old_type == new_type) { void* new_hd = DSTR_REALLOC(old_hd, new_hd_size + new_cap); - if(!new_hd) + if (!new_hd) return NULL; s = (char*)new_hd + new_hd_size; @@ -272,19 +284,19 @@ dstr dstrreserve(dstr s, size_t new_cap) _dstr_set_len(new_hd, s_len, new_type); _dstr_set_cap(new_hd, new_cap, new_type); - *((uint8_t*)new_hd + new_hd_size - 1) = (uint8_t)new_type; + *((uint8_t*)new_hd + new_hd_size - 1) = (uint8_t)new_type; return s; - } - + } + void* new_hd = DSTR_MALLOC(new_hd_size + new_cap); - if (!new_hd) + if (!new_hd) return NULL; _dstr_set_len(new_hd, s_len, new_type); _dstr_set_cap(new_hd, new_cap, new_type); - *((char*)new_hd + new_hd_size-1) = (uint8_t)new_type; + *((char*)new_hd + new_hd_size - 1) = (uint8_t)new_type; char* new_buf = (char*)new_hd + new_hd_size; memcpy(new_buf, s, s_len + 1); @@ -297,7 +309,8 @@ dstr dstrreserve(dstr s, size_t new_cap) // strcmp bool dstrequal(dstr s1, dstr s2) { - if(!s1 || !s2) return false; + if (!s1 || !s2) + return false; size_t s1_len = dstrlen(s1); if (s1_len != dstrlen(s2)) return false; @@ -306,25 +319,39 @@ bool dstrequal(dstr s1, dstr s2) } // strcat & strappend -static dstr _dstr_concat_impl(dstr s1, size_t s1_len, - const char* s2, size_t s2_len, - size_t cap) +static dstr _dstr_concat_impl(dstr s1, + size_t s1_len, + const char* s2, + size_t s2_len, + size_t cap) { - if (s2_len > SIZE_MAX - s1_len) return NULL; + if (s2_len > SIZE_MAX - s1_len) + return NULL; size_t new_len = s1_len + s2_len; - if (cap < new_len + 1) cap = new_len + 1; + if (cap < new_len + 1) + cap = new_len + 1; dstr tmp = dstrreserve(s1, cap); - if (!tmp) return NULL; + if (!tmp) + return NULL; s1 = tmp; uint8_t hdr_type = DSTRGETTYPE(s1); void* hd = NULL; switch (hdr_type) { - case DSTRHD_TYPE_8: hd = DSTRGETHDR(8, s1); break; - case DSTRHD_TYPE_16: hd = DSTRGETHDR(16, s1); break; - case DSTRHD_TYPE_32: hd = DSTRGETHDR(32, s1); break; - case DSTRHD_TYPE_64: hd = DSTRGETHDR(64, s1); break; - default: return NULL; + case DSTRHD_TYPE_8: + hd = DSTRGETHDR(8, s1); + break; + case DSTRHD_TYPE_16: + hd = DSTRGETHDR(16, s1); + break; + case DSTRHD_TYPE_32: + hd = DSTRGETHDR(32, s1); + break; + case DSTRHD_TYPE_64: + hd = DSTRGETHDR(64, s1); + break; + default: + return NULL; } memcpy(s1 + s1_len, s2, s2_len); s1[new_len] = '\0'; @@ -335,7 +362,8 @@ static dstr _dstr_concat_impl(dstr s1, size_t s1_len, // strcat dstr dstrcat_base(dstr s1, const char* s2) { - if (!s1 || !s2) return s1; + if (!s1 || !s2) + return s1; size_t s1_len = dstrlen(s1); size_t s2_len = strlen(s2); return _dstr_concat_impl(s1, s1_len, s2, s2_len, s1_len + s2_len + 1); @@ -343,30 +371,34 @@ dstr dstrcat_base(dstr s1, const char* s2) dstr dstrcat_custom(dstr s1, const char* s2, size_t cap) { - if (!s1 || !s2) return s1; + if (!s1 || !s2) + return s1; return _dstr_concat_impl(s1, dstrlen(s1), s2, strlen(s2), cap); } // strpush dstr dstrpush_custom(dstr s1, const char c, size_t cap) { - if(cap == 0 || !s1) return s1; + if (cap == 0 || !s1) + return s1; size_t s1_len = dstrlen(s1); size_t s1_cap = dstrcap(s1); - if(cap <= s1_cap) cap = s1_cap+1; + if (cap <= s1_cap) + cap = s1_cap + 1; - if(s1_cap <= s1_len + 1) { + if (s1_cap <= s1_len + 1) { s1 = dstrreserve(s1, cap); - if(!s1) return NULL; + if (!s1) + return NULL; } enum dstrhd_type s1_type; void* s1_hd = _dstr_get_hdr_and_type(s1, &s1_type); s1[s1_len] = c; - s1[s1_len+1] = '\0'; + s1[s1_len + 1] = '\0'; _dstr_set_len(s1_hd, s1_len + 1, s1_type); return s1; @@ -374,13 +406,14 @@ dstr dstrpush_custom(dstr s1, const char c, size_t cap) dstr dstrpush_base(dstr s1, const char c) { - return dstrpush_custom(s1, c, dstrcap(s1)+1); + return dstrpush_custom(s1, c, dstrcap(s1) + 1); } // strappend dstr dstrappend_base(dstr s1, const dstr s2) { - if (!s1 || !s2) return s1; + if (!s1 || !s2) + return s1; size_t s1_len = dstrlen(s1); size_t s2_len = dstrlen(s2); return _dstr_concat_impl(s1, s1_len, s2, s2_len, s1_len + s2_len + 1); @@ -388,17 +421,22 @@ dstr dstrappend_base(dstr s1, const dstr s2) dstr dstrappend_custom(dstr s1, const dstr s2, size_t cap) { - if (!s1 || !s2) return s1; + if (!s1 || !s2) + return s1; return _dstr_concat_impl(s1, dstrlen(s1), s2, dstrlen(s2), cap); } // strnew -static dstr _dstrnew_allocator(enum dstrhd_type t, const char* msg, size_t s_len, size_t alloc_size) +static dstr _dstrnew_allocator(enum dstrhd_type t, + const char* msg, + size_t s_len, + size_t alloc_size) { switch (t) { case DSTRHD_TYPE_8: { struct dstrhd8* hd = DSTR_MALLOC(sizeof(*hd) + alloc_size); - if (!hd) return NULL; + if (!hd) + return NULL; hd->cap = (uint8_t)alloc_size; hd->len = (uint8_t)s_len; @@ -410,7 +448,8 @@ static dstr _dstrnew_allocator(enum dstrhd_type t, const char* msg, size_t s_len case DSTRHD_TYPE_16: { struct dstrhd16* hd = DSTR_MALLOC(sizeof(*hd) + alloc_size); - if (!hd) return NULL; + if (!hd) + return NULL; hd->cap = (uint16_t)alloc_size; hd->len = (uint16_t)s_len; @@ -422,7 +461,8 @@ static dstr _dstrnew_allocator(enum dstrhd_type t, const char* msg, size_t s_len case DSTRHD_TYPE_32: { struct dstrhd32* hd = DSTR_MALLOC(sizeof(*hd) + alloc_size); - if (!hd) return NULL; + if (!hd) + return NULL; hd->cap = (uint32_t)alloc_size; hd->len = (uint32_t)s_len; @@ -434,7 +474,8 @@ static dstr _dstrnew_allocator(enum dstrhd_type t, const char* msg, size_t s_len case DSTRHD_TYPE_64: { struct dstrhd64* hd = DSTR_MALLOC(sizeof(*hd) + alloc_size); - if (!hd) return NULL; + if (!hd) + return NULL; hd->cap = (uint64_t)alloc_size; hd->len = (uint64_t)s_len; @@ -449,9 +490,10 @@ static dstr _dstrnew_allocator(enum dstrhd_type t, const char* msg, size_t s_len dstr dstrnew_base(const char* msg) { - if(!msg) return NULL; + if (!msg) + return NULL; size_t s_len = strlen(msg); - size_t alloc_size = s_len+1; + size_t alloc_size = s_len + 1; enum dstrhd_type t = _dstr_type_by_size(alloc_size); @@ -460,23 +502,24 @@ dstr dstrnew_base(const char* msg) dstr dstrnew_custom(const char* msg, size_t cap) { - if(!msg) return NULL; + if (!msg) + return NULL; size_t s_len = strlen(msg); size_t alloc_size = (s_len > cap) ? (s_len + 1) : cap; enum dstrhd_type t = _dstr_type_by_size(alloc_size); return _dstrnew_allocator(t, msg, s_len, alloc_size); - } // strfree void dstrfree(dstr s) { - if(!s) return; + if (!s) + return; enum dstrhd_type t = s[-1]; - switch(t) { + switch (t) { case DSTRHD_TYPE_8: { free(DSTRGETHDR(8, s)); return; @@ -493,6 +536,5 @@ void dstrfree(dstr s) free(DSTRGETHDR(64, s)); return; } - } } diff --git a/dstr.h b/dstr.h index a1703ec..06e1b41 100644 --- a/dstr.h +++ b/dstr.h @@ -12,39 +12,37 @@ #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L #error "DynamicStringLibrary supports only C11+ versions" #endif -#if !defined(__x86_64__) && !defined(_M_X64) && \ - !defined(__i386__) && !defined(_M_IX86) && \ - !defined(__aarch64__) && !defined(__arm__) -#error "DynamicStringLibrary supports only common architectures: x86, x86-64, ARM, ARM64" +#if !defined(__x86_64__) && !defined(_M_X64) && !defined(__i386__) && \ + !defined(_M_IX86) && !defined(__aarch64__) && !defined(__arm__) +#error \ + "DynamicStringLibrary supports only common architectures: x86, x86-64, ARM, ARM64" #endif -#include #include +#include #define DSTR_SHORTCUT_ENABLED 0 /* 1 enabled | 0 disabled */ #define GET_DSTRNEW(_1, _2, NAME, ...) NAME -#define dstrnew(...) \ +#define dstrnew(...) \ GET_DSTRNEW(__VA_ARGS__, dstrnew_custom, dstrnew_base)(__VA_ARGS__) -#define GET_DSTRPUSH(_1,_2,_3,NAME,...) NAME -#define dstrpush(...) \ +#define GET_DSTRPUSH(_1, _2, _3, NAME, ...) NAME +#define dstrpush(...) \ GET_DSTRPUSH(__VA_ARGS__, dstrpush_custom, dstrpush_base)(__VA_ARGS__) -#define GET_DSTRCAT(_1,_2,_3,NAME,...) NAME -#define dstrcat(...) \ +#define GET_DSTRCAT(_1, _2, _3, NAME, ...) NAME +#define dstrcat(...) \ GET_DSTRCAT(__VA_ARGS__, dstrcat_custom, dstrcat_base)(__VA_ARGS__) -#define GET_DSTRAPPEND(_1,_2,_3,NAME,...) NAME -#define dstrappend(...) \ +#define GET_DSTRAPPEND(_1, _2, _3, NAME, ...) NAME +#define dstrappend(...) \ GET_DSTRAPPEND(__VA_ARGS__, dstrappend_custom, dstrappend_base)(__VA_ARGS__) -#define W_UNUSED_RESULT \ - __attribute__((warn_unused_result)) +#define W_UNUSED_RESULT __attribute__((warn_unused_result)) typedef char* dstr; - /* * dstrnew(msg) * dstrnew(msg, cap) @@ -154,7 +152,11 @@ dstr dstrappend_custom(dstr s1, const dstr s2, size_t cap) W_UNUSED_RESULT; * dstrauto s = dstrnew("hello"); */ #define dstrauto __attribute__((cleanup(_dstr_autofree))) dstr -static inline void _dstr_autofree(dstr* s) { if (*s) dstrfree(*s); } +static inline void _dstr_autofree(dstr* s) +{ + if (*s) + dstrfree(*s); +} /* * $(msg) @@ -164,14 +166,10 @@ static inline void _dstr_autofree(dstr* s) { if (*s) dstrfree(*s); } * in dstr_options.h. */ #if DSTR_SHORTCUT_ENABLED - #define GET_DOLLAR(_1,_2,NAME,...) NAME - #define $(...) \ - GET_DOLLAR(__VA_ARGS__, $_custom, $_base)(__VA_ARGS__) - #define $_base(str) \ - dstrnew_base(str) - #define $_custom(str, cap) \ - dstrnew_custom(str, cap) +#define GET_DOLLAR(_1, _2, NAME, ...) NAME +#define $(...) GET_DOLLAR(__VA_ARGS__, $_custom, $_base)(__VA_ARGS__) +#define $_base(str) dstrnew_base(str) +#define $_custom(str, cap) dstrnew_custom(str, cap) #endif - #endif