From bc6ebd920a2e64423a7eaebb63d6efdd070f28d7 Mon Sep 17 00:00:00 2001 From: halx99 Date: Thu, 15 Jul 2021 23:03:43 +0800 Subject: [PATCH] Improve code quality --- .clang-format | 240 +++++-------------- cocos/base/ZipUtils.cpp | 14 +- cocos/platform/android/CCDevice-android.cpp | 9 +- cocos/platform/win32/CCDevice-win32.cpp | 6 +- cocos/ui/UIEditBox/UIEditBoxImpl-android.cpp | 9 +- cocos/ui/UIWebView/UIWebViewImpl-android.cpp | 28 ++- extensions/Particle3D/PU/CCPUPlane.cpp | 8 +- 7 files changed, 99 insertions(+), 215 deletions(-) diff --git a/.clang-format b/.clang-format index 61144b5e8f..b069bdcce5 100644 --- a/.clang-format +++ b/.clang-format @@ -1,186 +1,62 @@ -# This file is copy from https://github.com/microsoft/STL with some modifications -# Copyright (c) Microsoft Corporation. -# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# This file is copy from https://github.com/google/angle with some modifications +# Defines the ADXE style for automatic reformatting. +# https://code.google.com/p/angleproject/wiki/CodingStandard +# See Clang docs: http://clang.llvm.org/docs/ClangFormatStyleOptions.html +BasedOnStyle: Chromium -# https://releases.llvm.org/12.0.0/tools/clang/docs/ClangFormatStyleOptions.html - ---- -Language: Cpp +# Allow double brackets such as std::vector>. Standard: Cpp11 -BasedOnStyle: LLVM -# AccessModifierOffset: -2 -AccessModifierOffset: -4 -# AlignAfterOpenBracket: Align -AlignAfterOpenBracket: DontAlign -# AlignConsecutiveMacros: false -AlignConsecutiveMacros: true -# AlignConsecutiveAssignments: false + +# Indent 4 spaces at a time. +IndentWidth: 4 + +# Keep lines under 120 columns long. +ColumnLimit: 120 + +# Always break before braces +BreakBeforeBraces: Custom +BraceWrapping: + AfterCaseLabel: true + AfterClass: true + AfterControlStatement: true + AfterEnum: true + AfterFunction: true + AfterNamespace: true + AfterStruct: true + AfterUnion: true + BeforeCatch: true + BeforeElse: true + IndentBraces: false + SplitEmptyFunction: false + SplitEmptyRecord: false + SplitEmptyNamespace: false + + # Keeps extern "C" blocks unindented. + AfterExternBlock: false + +# Indent case labels. +IndentCaseLabels: true + +# Right-align pointers and references +PointerAlignment: Right + +# ANGLE likes to align things as much as possible. +AlignOperands: true AlignConsecutiveAssignments: true -# AlignConsecutiveBitFields: false -# AlignConsecutiveDeclarations: false -# AlignEscapedNewlines: Right -AlignEscapedNewlines: Left -# AlignOperands: Align -AlignOperands: AlignAfterOperator -# AlignTrailingComments: true -AlignTrailingComments: false -# AllowAllArgumentsOnNextLine: true -# AllowAllConstructorInitializersOnNextLine: true -# AllowAllParametersOfDeclarationOnNextLine: true -# AllowShortEnumsOnASingleLine: true -# AllowShortBlocksOnASingleLine: Never -# AllowShortCaseLabelsOnASingleLine: false -# AllowShortFunctionsOnASingleLine: All -AllowShortFunctionsOnASingleLine: Empty -# AllowShortLambdasOnASingleLine: All -# AllowShortIfStatementsOnASingleLine: Never -# AllowShortLoopsOnASingleLine: false -# AlwaysBreakAfterDefinitionReturnType: None -# AlwaysBreakAfterReturnType: None -# AlwaysBreakBeforeMultilineStrings: false -# AlwaysBreakTemplateDeclarations: MultiLine -AlwaysBreakTemplateDeclarations: Yes -# BinPackArguments: true -# BinPackParameters: true -# BraceWrapping: -# AfterCaseLabel: false -# AfterClass: false -# AfterControlStatement: Never -# AfterEnum: false -# AfterFunction: false -# AfterNamespace: false -# AfterObjCDeclaration: false -# AfterStruct: false -# AfterUnion: false -# AfterExternBlock: false -# BeforeCatch: false -# BeforeElse: false -# BeforeLambdaBody: false -# BeforeWhile: false -# IndentBraces: false -# SplitEmptyFunction: true -# SplitEmptyRecord: true -# SplitEmptyNamespace: true -# BreakBeforeBinaryOperators: None -BreakBeforeBinaryOperators: NonAssignment -# BreakBeforeBraces: Attach -# BreakBeforeInheritanceComma: false -# BreakInheritanceList: BeforeColon -# BreakBeforeTernaryOperators: true -# BreakConstructorInitializersBeforeComma: false -# BreakConstructorInitializers: BeforeColon -# BreakAfterJavaFieldAnnotations: false -# BreakStringLiterals: true -# ColumnLimit: 80 -ColumnLimit: 120 -# CommentPragmas: '^ IWYU pragma:' -# CompactNamespaces: false -# ConstructorInitializerAllOnOneLineOrOnePerLine: false -# ConstructorInitializerIndentWidth: 4 -# ContinuationIndentWidth: 4 -# Cpp11BracedListStyle: true -# DeriveLineEnding: true -DeriveLineEnding: false -# DerivePointerAlignment: false -# DisableFormat: false -# ExperimentalAutoDetectBinPacking: false -# FixNamespaceComments: true -# ForEachMacros: -# - foreach -# - Q_FOREACH -# - BOOST_FOREACH -# IncludeBlocks: Preserve -IncludeBlocks: Regroup -# IncludeCategories: -# - Regex: '^"(llvm|llvm-c|clang|clang-c)/' -# Priority: 2 -# SortPriority: 0 -# - Regex: '^(<|"(gtest|gmock|isl|json)/)' -# Priority: 3 -# SortPriority: 0 -# - Regex: '.*' -# Priority: 1 -# SortPriority: 0 -IncludeCategories: - - Regex: '^$' - Priority: 1 - - Regex: '^<(Windows|userenv)\.h>$' - Priority: 3 - SortPriority: 3 - - Regex: '^$' - Priority: 3 - SortPriority: 4 - - Regex: '^<__.*\.hpp>$' - Priority: 2 - - Regex: '\.hpp[>"]$' - Priority: 5 - - Regex: '.*' - Priority: 2 -# IncludeIsMainRegex: '(Test)?$' -# IncludeIsMainSourceRegex: '' -# IndentCaseLabels: false -# IndentCaseBlocks: false -IndentCaseBlocks: true -# IndentGotoLabels: true -# IndentPPDirectives: None -# IndentExternBlock: AfterExternBlock -# IndentWidth: 2 -IndentWidth: 4 -# IndentWrappedFunctionNames: false -IndentWrappedFunctionNames: true -# InsertTrailingCommas: None -# JavaScriptQuotes: Leave -# JavaScriptWrapImports: true -# KeepEmptyLinesAtTheStartOfBlocks: true -# NOTE: MacroBlockBegin/MacroBlockEnd don't work with _CATCH_ALL. -# MacroBlockBegin: '' -# MacroBlockEnd: '' -# MaxEmptyLinesToKeep: 1 -MaxEmptyLinesToKeep: 2 -# NamespaceIndentation: None -NamespaceIndentation: None -# ObjCBinPackProtocolList: Auto -# ObjCBlockIndentWidth: 2 -# ObjCBreakBeforeNestedBlockParam: true -# ObjCSpaceAfterProperty: false -# ObjCSpaceBeforeProtocolList: true -# PenaltyBreakAssignment: 2 -# PenaltyBreakBeforeFirstCallParameter: 19 -# PenaltyBreakComment: 300 -# PenaltyBreakFirstLessLess: 120 -# PenaltyBreakString: 1000 -# PenaltyBreakTemplateDeclaration: 10 -# PenaltyExcessCharacter: 1000000 -# PenaltyReturnTypeOnItsOwnLine: 60 -# PointerAlignment: Right -PointerAlignment: Left -# ReflowComments: true -SortIncludes: false -# SortUsingDeclarations: true -# SpaceAfterCStyleCast: false -SpaceAfterCStyleCast: true -# SpaceAfterLogicalNot: false -# SpaceAfterTemplateKeyword: true -# SpaceBeforeAssignmentOperators: true -# SpaceBeforeCpp11BracedList: false -# SpaceBeforeCtorInitializerColon: true -# SpaceBeforeInheritanceColon: true -# SpaceBeforeParens: ControlStatements -# SpaceBeforeRangeBasedForLoopColon: true -# SpaceInEmptyBlock: false -# SpaceInEmptyParentheses: false -# SpacesBeforeTrailingComments: 1 -# SpacesInAngles: false -# SpacesInConditionalStatement: false -# SpacesInContainerLiterals: true -# SpacesInCStyleCastParentheses: false -# SpacesInParentheses: false -# SpacesInSquareBrackets: false -# SpaceBeforeSquareBrackets: false -# Standard: Latest -# StatementMacros: -# - Q_UNUSED -# - QT_REQUIRE_VERSION -# NOTE: _STD_BEGIN, _STD_END, etc. aren't macros for complete statements, but telling -# clang-format that they are produces the behavior that we want (with no block indentation). + +# Use 2 space negative offset for access modifiers +AccessModifierOffset: -2 + +# TODO(jmadill): Decide if we want this on. Doesn't have an "all or none" mode. +AllowShortCaseLabelsOnASingleLine: false + +# Useful for spacing out functions in classes +KeepEmptyLinesAtTheStartOfBlocks: true + +# Indent nested PP directives. +IndentPPDirectives: AfterHash + +# Include blocks style +IncludeBlocks: Preserve UseTab: Never +BreakConstructorInitializers: BeforeComma \ No newline at end of file diff --git a/cocos/base/ZipUtils.cpp b/cocos/base/ZipUtils.cpp index c8c16a7f67..bd98212bb0 100644 --- a/cocos/base/ZipUtils.cpp +++ b/cocos/base/ZipUtils.cpp @@ -2,8 +2,9 @@ Copyright (c) 2010-2012 cocos2d-x.org Copyright (c) 2013-2016 Chukong Technologies Inc. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. + Copyright (c) 2021 Bytedance Inc. - http://www.cocos2d-x.org + https://adxe.org Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -47,6 +48,8 @@ #include #include +#include "yasio/cxx17/string_view.hpp" + // minizip 1.2.0 is same with other platforms #define unzGoToFirstFile64(A,B,C,D) unzGoToFirstFile2(A,B,C,D, NULL, 0, NULL, 0) #define unzGoToNextFile64(A,B,C,D) unzGoToNextFile2(A,B,C,D, NULL, 0, NULL, 0) @@ -736,10 +739,10 @@ std::vector ZipFile::listFiles(const std::string &pathname) const ZipFilePrivate::FileListContainer::const_iterator end = _data->fileList.end(); //ensure pathname ends with `/` as a directory std::string dirname = pathname[pathname.length() -1] == '/' ? pathname : pathname + "/"; - while(it != end) + for(auto& item : _data->fileList) { - const std::string &filename = it->first; - if(filename.substr(0, dirname.length()) == dirname) + const std::string &filename = item.first; + if (cxx20::starts_with(cxx17::string_view{filename}, cxx17::string_view{dirname})) { std::string suffix = filename.substr(dirname.length()); auto pos = suffix.find('/'); @@ -752,10 +755,9 @@ std::vector ZipFile::listFiles(const std::string &pathname) const fileSet.insert(suffix.substr(0, pos + 1)); } } - it++; } - return std::vector(fileSet.begin(), fileSet.end()); + return std::vector{fileSet.begin(), fileSet.end()}; } unsigned char *ZipFile::getFileData(const std::string &fileName, ssize_t *size) diff --git a/cocos/platform/android/CCDevice-android.cpp b/cocos/platform/android/CCDevice-android.cpp index 46a068b3f5..87082f1e7e 100644 --- a/cocos/platform/android/CCDevice-android.cpp +++ b/cocos/platform/android/CCDevice-android.cpp @@ -2,8 +2,9 @@ Copyright (c) 2010-2012 cocos2d-x.org Copyright (c) 2013-2016 Chukong Technologies Inc. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. +Copyright (c) 2021 Bytedance Inc. -http://www.cocos2d-x.org +https://adxe.org Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -30,6 +31,7 @@ THE SOFTWARE. #include "base/ccTypes.h" #include "platform/android/jni/JniHelper.h" #include "platform/CCFileUtils.h" +#include "yasio/cxx17/string_view.hpp" static const std::string helperClassName = "org.cocos2dx.lib.Cocos2dxHelper"; @@ -98,9 +100,10 @@ public: fullPathOrFontName = FileUtils::getInstance()->fullPathForFilename(textDefinition._fontName); // If the path name returned includes the 'assets' dir then that needs to be removed, because the android.content.Context // requires this portion of the path to be omitted for assets inside the app package. - if (fullPathOrFontName.find("assets/") == 0) + using namespace cxx17; + if (cxx20::starts_with(cxx17::string_view { fullPathOrFontName }, "assets/"_sv)) { - fullPathOrFontName = fullPathOrFontName.substr(strlen("assets/")); // Chop out the 'assets/' portion of the path. + fullPathOrFontName = fullPathOrFontName.substr(sizeof("assets/") - 1); // Chop out the 'assets/' portion of the path. } } diff --git a/cocos/platform/win32/CCDevice-win32.cpp b/cocos/platform/win32/CCDevice-win32.cpp index 2e37546ed1..275e401da4 100644 --- a/cocos/platform/win32/CCDevice-win32.cpp +++ b/cocos/platform/win32/CCDevice-win32.cpp @@ -2,8 +2,9 @@ Copyright (c) 2010-2012 cocos2d-x.org Copyright (c) 2013-2016 Chukong Technologies Inc. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. +Copyright (c) 2021 Bytedance Inc. -http://www.cocos2d-x.org +https://adxe.org Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -58,9 +59,8 @@ public: : _DC(nullptr) , _bmp(nullptr) , _font((HFONT)GetStockObject(DEFAULT_GUI_FONT)) - , _wnd(nullptr) + , _wnd(hWnd) { - _wnd = hWnd; HDC hdc = GetDC(hWnd); _DC = CreateCompatibleDC(hdc); ReleaseDC(hWnd, hdc); diff --git a/cocos/ui/UIEditBox/UIEditBoxImpl-android.cpp b/cocos/ui/UIEditBox/UIEditBoxImpl-android.cpp index fdde8b43cf..847f839179 100644 --- a/cocos/ui/UIEditBox/UIEditBoxImpl-android.cpp +++ b/cocos/ui/UIEditBox/UIEditBoxImpl-android.cpp @@ -3,8 +3,9 @@ Copyright (c) 2012 James Chen Copyright (c) 2013-2015 zilongshanren Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. + Copyright (c) 2021 Bytedance Inc. - http://www.cocos2d-x.org + https://adxe.org Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -38,6 +39,7 @@ #include "ui/UIHelper.h" #include "base/CCDirector.h" #include "platform/CCFileUtils.h" +#include "yasio/cxx17/string_view.hpp" NS_CC_BEGIN @@ -118,9 +120,10 @@ void EditBoxImplAndroid::setNativeFont(const char* pFontName, int fontSize) std::string realFontPath = pFontName; if(isFontFileExists) { realFontPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(pFontName); - if (realFontPath.find("assets/") == 0) + using namespace cxx17; // for cxx17::string_view literal + if (cxx20::starts_with(cxx17::string_view{realFontPath}, "assets/"_sv)) { - realFontPath = realFontPath.substr(strlen("assets/")); // Chop out the 'assets/' portion of the path. + realFontPath = realFontPath.substr(sizeof("assets/") - 1); // Chop out the 'assets/' portion of the path. } } JniHelper::callStaticVoidMethod(editBoxClassName, "setFont", diff --git a/cocos/ui/UIWebView/UIWebViewImpl-android.cpp b/cocos/ui/UIWebView/UIWebViewImpl-android.cpp index 8a0260ae61..92794f50ec 100644 --- a/cocos/ui/UIWebView/UIWebViewImpl-android.cpp +++ b/cocos/ui/UIWebView/UIWebViewImpl-android.cpp @@ -1,8 +1,9 @@ /**************************************************************************** Copyright (c) 2014-2016 Chukong Technologies Inc. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. - - http://www.cocos2d-x.org + Copyright (c) 2021 Bytedance Inc. + + https://adxe.org Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -34,6 +35,7 @@ #include "base/CCDirector.h" #include "platform/CCFileUtils.h" #include "ui/UIHelper.h" +#include "yasio/cxx17/string_view.hpp" static const std::string className = "org.cocos2dx.lib.Cocos2dxWebViewHelper"; @@ -54,7 +56,8 @@ static std::string getFixedBaseUrl(const std::string& baseUrl) fixedBaseUrl = baseUrl; } else if (baseUrl.c_str()[0] != '/') { - if(baseUrl.find("assets/") == 0) { + using namespace cxx17; // for cxx17::string_view literal + if(cxx20::starts_with(cxx17::string_view{baseUrl}, "assets/"_sv)) { fixedBaseUrl = s_defaultBaseUrl + baseUrl.c_str()[7]; } else { @@ -138,25 +141,24 @@ int createWebViewJNI() { return -1; } -std::string getUrlStringByFileName(const std::string &fileName) { +std::string getUrlStringByFileName(const std::string& fileName) { // LOGD("error: %s,%d",__func__,__LINE__); const std::string basePath("file:///android_asset/"); - const std::string assetsPath("assets/"); + + using namespace cxx17; + const auto assetsPath = "assets/"_sv; std::string fullPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(fileName); std::string urlString; if (fullPath.empty()) { return urlString; - } - else if (fullPath[0] == '/') { + } else if (fullPath[0] == '/') { urlString.append("file://").append(fullPath); - } - else if (fullPath.find(assetsPath) == 0) { - urlString = fullPath.replace(fullPath.find_first_of(assetsPath), assetsPath.length(), assetsPath); - } - else { + } else if (cxx20::starts_with(cxx17::string_view{fullPath}, assetsPath)) { + urlString = fullPath; + } else { urlString.append(basePath).append(fullPath); } - + return urlString; } } // namespace diff --git a/extensions/Particle3D/PU/CCPUPlane.cpp b/extensions/Particle3D/PU/CCPUPlane.cpp index 93f004526f..b18e2237fe 100644 --- a/extensions/Particle3D/PU/CCPUPlane.cpp +++ b/extensions/Particle3D/PU/CCPUPlane.cpp @@ -2,9 +2,9 @@ Copyright (C) 2013 Henry van Merode. All rights reserved. Copyright (c) 2015-2016 Chukong Technologies Inc. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. + Copyright (c) 2021 Bytedance Inc. - http://www.cocos2d-x.org - + https://adxe.org 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 @@ -35,10 +35,8 @@ PUPlane::PUPlane () d = 0.0; } //----------------------------------------------------------------------- -PUPlane::PUPlane (const PUPlane& rhs) +PUPlane::PUPlane(const PUPlane& rhs) : normal(rhs.normal), d(rhs.d) { - normal = rhs.normal; - d = rhs.d; } //----------------------------------------------------------------------- PUPlane::PUPlane (const Vec3& rkNormal, float fConstant)