From 9022cb435a91083c86633d6672f92ca6643cb229 Mon Sep 17 00:00:00 2001 From: pkasting Date: Thu, 4 Feb 2016 16:08:56 -0800 Subject: Allow string::front() and back() and update the codebase to use back(). I searched for "length() - 1]", which won't find all potential sites to use back(), but should hit a reasonable number. These were all converted. I don't consider str.front() to be vastly more readable than str[0], so I made no effort to seek these out and change them. I did change code to use front() when it would make for better parallel structure with a place I was making use back(), e.g.: if (str[0] == 'x' && str[str.length() - 1] == 'y') { ...was transformed to: if (str.front() == 'x' && str.back() == 'y') { ...and not: if (str[0] == 'x' && str.back() == 'y') { I also added front() and back() methods to StringPiece so people wouldn't need to distinguish between string and StringPiece for these purposes. BUG=none TEST=none Review URL: https://codereview.chromium.org/1663253004 Cr-Commit-Position: refs/heads/master@{#373672} --- base/files/file_path.cc | 4 ++-- base/mac/foundation_util.mm | 2 +- base/strings/string_piece.h | 2 ++ base/sys_info_linux.cc | 2 +- .../browsing_data/cookies_tree_model_unittest.cc | 2 +- .../browser/extensions/extension_creator_filter.cc | 5 ++--- chrome/browser/policy/policy_path_parser_linux.cc | 4 ++-- chrome/browser/policy/policy_path_parser_mac.mm | 4 ++-- chrome/browser/policy/policy_path_parser_win.cc | 4 ++-- chrome/common/cloud_print/cloud_print_helpers.cc | 2 +- .../cloud_print/cloud_print_helpers_unittest.cc | 2 +- .../cloud_print_service_helpers_unittest.cc | 2 +- .../chromedriver/server/chromedriver_server.cc | 4 ++-- chromeos/disks/disk_mount_manager.cc | 2 +- .../core/browser/webdata/autofill_table.cc | 2 +- .../serialization/serialization_utils_unittest.cc | 2 +- components/omnibox/browser/history_provider.cc | 3 +-- components/search_engines/template_url_service.cc | 4 ++-- content/child/multipart_response_delegate.cc | 2 +- .../common/permissions/socket_permission_entry.cc | 4 ++-- .../client/program_info_manager_unittest.cc | 2 +- media/filters/video_cadence_estimator_unittest.cc | 4 ++-- net/base/filename_util_internal.cc | 4 ++-- net/cookies/canonical_cookie.cc | 3 +-- net/cookies/cookie_monster_unittest.cc | 2 +- net/ftp/ftp_directory_listing_parser_vms.cc | 4 ++-- net/ftp/ftp_network_transaction.cc | 4 ++-- net/ftp/ftp_util.cc | 4 ++-- net/proxy/proxy_config_service_linux.cc | 2 +- net/tools/balsa/balsa_frame.cc | 4 ++-- net/tools/flip_server/url_to_filename_encoder.h | 2 +- net/tools/quic/quic_in_memory_cache.cc | 2 +- sandbox/win/src/win_utils.cc | 2 +- storage/browser/fileapi/external_mount_points.cc | 2 +- styleguide/c++/c++11.html | 22 +++++++++++----------- ui/gfx/text_elider.cc | 2 +- ui/shell_dialogs/select_file_dialog_win.cc | 2 +- 37 files changed, 62 insertions(+), 63 deletions(-) diff --git a/base/files/file_path.cc b/base/files/file_path.cc index 63e9387..5756dfc 100644 --- a/base/files/file_path.cc +++ b/base/files/file_path.cc @@ -502,10 +502,10 @@ FilePath FilePath::Append(StringPieceType component) const { // Don't append a separator if the path is empty (indicating the current // directory) or if the path component is empty (indicating nothing to // append). - if (appended.length() > 0 && new_path.path_.length() > 0) { + if (!appended.empty() && !new_path.path_.empty()) { // Don't append a separator if the path still ends with a trailing // separator after stripping (indicating the root directory). - if (!IsSeparator(new_path.path_[new_path.path_.length() - 1])) { + if (!IsSeparator(new_path.path_.back())) { // Don't append a separator if the path is just a drive letter. if (FindDriveLetter(new_path.path_) + 1 != new_path.path_.length()) { new_path.path_.append(1, kSeparators[0]); diff --git a/base/mac/foundation_util.mm b/base/mac/foundation_util.mm index 524f17c..6ae5df3 100644 --- a/base/mac/foundation_util.mm +++ b/base/mac/foundation_util.mm @@ -165,7 +165,7 @@ FilePath GetAppBundlePath(const FilePath& exec_name) { // The first component may be "/" or "//", etc. Only append '/' if it doesn't // already end in '/'. - if (bundle_name[bundle_name.length() - 1] != '/') + if (bundle_name.back() != '/') bundle_name += '/'; // Go through the remaining components. diff --git a/base/strings/string_piece.h b/base/strings/string_piece.h index 92634b9..20784d7 100644 --- a/base/strings/string_piece.h +++ b/base/strings/string_piece.h @@ -224,6 +224,8 @@ template class BasicStringPiece { } value_type operator[](size_type i) const { return ptr_[i]; } + value_type front() const { return ptr_[0]; } + value_type back() const { return ptr_[length_ - 1]; } void remove_prefix(size_type n) { ptr_ += n; diff --git a/base/sys_info_linux.cc b/base/sys_info_linux.cc index 8e1f533..300ef2c0 100644 --- a/base/sys_info_linux.cc +++ b/base/sys_info_linux.cc @@ -37,7 +37,7 @@ uint64_t MaxSharedMemorySize() { std::string contents; base::ReadFileToString(base::FilePath("/proc/sys/kernel/shmmax"), &contents); DCHECK(!contents.empty()); - if (!contents.empty() && contents[contents.length() - 1] == '\n') { + if (!contents.empty() && contents.back() == '\n') { contents.erase(contents.length() - 1); } diff --git a/chrome/browser/browsing_data/cookies_tree_model_unittest.cc b/chrome/browser/browsing_data/cookies_tree_model_unittest.cc index ac1724c..22f0766 100644 --- a/chrome/browser/browsing_data/cookies_tree_model_unittest.cc +++ b/chrome/browser/browsing_data/cookies_tree_model_unittest.cc @@ -353,7 +353,7 @@ class CookiesTreeModelTest : public testing::Test { CookieTreeRootNode* root = static_cast( cookies_model->GetRoot()); std::string retval = GetNodesOfChildren(root, type); - if (retval.length() && retval[retval.length() - 1] == ',') + if (!retval.empty() && retval.back() == ',') retval.erase(retval.length() - 1); return retval; } diff --git a/chrome/browser/extensions/extension_creator_filter.cc b/chrome/browser/extensions/extension_creator_filter.cc index 330557f2..85efaa1 100644 --- a/chrome/browser/extensions/extension_creator_filter.cc +++ b/chrome/browser/extensions/extension_creator_filter.cc @@ -45,9 +45,8 @@ bool ExtensionCreatorFilter::ShouldPackageFile( return false; } - base::FilePath::CharType first_character = base_name.value()[0]; - base::FilePath::CharType last_character = - base_name.value()[base_name.value().length() - 1]; + base::FilePath::CharType first_character = base_name.value().front(); + base::FilePath::CharType last_character = base_name.value().back(); // dotfile if (first_character == '.') { diff --git a/chrome/browser/policy/policy_path_parser_linux.cc b/chrome/browser/policy/policy_path_parser_linux.cc index 6dc2af4..9b0e6fd 100644 --- a/chrome/browser/policy/policy_path_parser_linux.cc +++ b/chrome/browser/policy/policy_path_parser_linux.cc @@ -27,8 +27,8 @@ base::FilePath::StringType ExpandPathVariables( return result; // Sanitize quotes in case of any around the whole string. if (result.length() > 1 && - ((result[0] == '"' && result[result.length() - 1] == '"') || - (result[0] == '\'' && result[result.length() - 1] == '\''))) { + ((result.front() == '"' && result.back() == '"') || + (result.front() == '\'' && result.back() == '\''))) { // Strip first and last char which should be matching quotes now. result = result.substr(1, result.length() - 2); } diff --git a/chrome/browser/policy/policy_path_parser_mac.mm b/chrome/browser/policy/policy_path_parser_mac.mm index 1ac9558..24275f6 100644 --- a/chrome/browser/policy/policy_path_parser_mac.mm +++ b/chrome/browser/policy/policy_path_parser_mac.mm @@ -48,8 +48,8 @@ base::FilePath::StringType ExpandPathVariables( return result; // Sanitize quotes in case of any around the whole string. if (result.length() > 1 && - ((result[0] == '"' && result[result.length() - 1] == '"') || - (result[0] == '\'' && result[result.length() - 1] == '\''))) { + ((result.front() == '"' && result.back() == '"') || + (result.front() == '\'' && result.back() == '\''))) { // Strip first and last char which should be matching quotes now. result = result.substr(1, result.length() - 2); } diff --git a/chrome/browser/policy/policy_path_parser_win.cc b/chrome/browser/policy/policy_path_parser_win.cc index d8c62c7..6a1888d 100644 --- a/chrome/browser/policy/policy_path_parser_win.cc +++ b/chrome/browser/policy/policy_path_parser_win.cc @@ -73,8 +73,8 @@ base::FilePath::StringType ExpandPathVariables( return result; // Sanitize quotes in case of any around the whole string. if (result.length() > 1 && - ((result[0] == L'"' && result[result.length() - 1] == L'"') || - (result[0] == L'\'' && result[result.length() - 1] == L'\''))) { + ((result.front() == L'"' && result.back() == L'"') || + (result.front() == L'\'' && result.back() == L'\''))) { // Strip first and last char which should be matching quotes now. result = result.substr(1, result.length() - 2); } diff --git a/chrome/common/cloud_print/cloud_print_helpers.cc b/chrome/common/cloud_print/cloud_print_helpers.cc index 6fcd8bd..a5a450e 100644 --- a/chrome/common/cloud_print/cloud_print_helpers.cc +++ b/chrome/common/cloud_print/cloud_print_helpers.cc @@ -52,7 +52,7 @@ std::string HashPrinterTags(const PrinterTags& printer_tags) { std::string AppendPathToUrl(const GURL& url, const std::string& path) { DCHECK_NE(path[0], '/'); std::string ret = url.path(); - if (url.has_path() && (ret[ret.length() - 1] != '/')) + if (url.has_path() && (ret.back() != '/')) ret += '/'; ret += path; return ret; diff --git a/chrome/common/cloud_print/cloud_print_helpers_unittest.cc b/chrome/common/cloud_print/cloud_print_helpers_unittest.cc index 41b8f98..2792218 100644 --- a/chrome/common/cloud_print/cloud_print_helpers_unittest.cc +++ b/chrome/common/cloud_print/cloud_print_helpers_unittest.cc @@ -17,7 +17,7 @@ namespace { void CheckURLs(const GURL& server_base_url) { std::string expected_url_base = server_base_url.spec(); - if (expected_url_base[expected_url_base.length() - 1] != '/') + if (expected_url_base.back() != '/') expected_url_base += "/"; EXPECT_EQ(base::StringPrintf("%ssearch", expected_url_base.c_str()), diff --git a/chrome/service/cloud_print/cloud_print_service_helpers_unittest.cc b/chrome/service/cloud_print/cloud_print_service_helpers_unittest.cc index 761e18f..b41fb08 100644 --- a/chrome/service/cloud_print/cloud_print_service_helpers_unittest.cc +++ b/chrome/service/cloud_print/cloud_print_service_helpers_unittest.cc @@ -16,7 +16,7 @@ namespace { void CheckJobStatusURLs(const GURL& server_base_url) { std::string expected_url_base = server_base_url.spec(); - if (expected_url_base[expected_url_base.length() - 1] != '/') + if (expected_url_base.back() != '/') expected_url_base += "/"; EXPECT_EQ(base::StringPrintf( diff --git a/chrome/test/chromedriver/server/chromedriver_server.cc b/chrome/test/chromedriver/server/chromedriver_server.cc index 3158795..61db7c4 100644 --- a/chrome/test/chromedriver/server/chromedriver_server.cc +++ b/chrome/test/chromedriver/server/chromedriver_server.cc @@ -285,9 +285,9 @@ int main(int argc, char *argv[]) { } if (cmd_line->HasSwitch("url-base")) url_base = cmd_line->GetSwitchValueASCII("url-base"); - if (url_base.empty() || url_base[0] != '/') + if (url_base.empty() || url_base.front() != '/') url_base = "/" + url_base; - if (url_base[url_base.length() - 1] != '/') + if (url_base.back() != '/') url_base = url_base + "/"; if (cmd_line->HasSwitch("whitelisted-ips")) { allow_remote = true; diff --git a/chromeos/disks/disk_mount_manager.cc b/chromeos/disks/disk_mount_manager.cc index 3c89ac6..d4edfea 100644 --- a/chromeos/disks/disk_mount_manager.cc +++ b/chromeos/disks/disk_mount_manager.cc @@ -275,7 +275,7 @@ class DiskMountManagerImpl : public DiskMountManager { void UnmountChildMounts(const std::string& mount_path_in) { std::string mount_path = mount_path_in; // Let's make sure mount path has trailing slash. - if (mount_path[mount_path.length() - 1] != '/') + if (mount_path.back() != '/') mount_path += '/'; for (MountPointMap::iterator it = mount_points_.begin(); diff --git a/components/autofill/core/browser/webdata/autofill_table.cc b/components/autofill/core/browser/webdata/autofill_table.cc index cc4f6a8..07a291c 100644 --- a/components/autofill/core/browser/webdata/autofill_table.cc +++ b/components/autofill/core/browser/webdata/autofill_table.cc @@ -495,7 +495,7 @@ bool AutofillTable::GetFormValuesForElementName( } else { base::string16 prefix_lower = base::i18n::ToLower(prefix); base::string16 next_prefix = prefix_lower; - next_prefix[next_prefix.length() - 1]++; + next_prefix.back()++; sql::Statement s1; s1.Assign(db_->GetUniqueStatement( diff --git a/components/metrics/serialization/serialization_utils_unittest.cc b/components/metrics/serialization/serialization_utils_unittest.cc index 9c8f9de..fe8f2b2 100644 --- a/components/metrics/serialization/serialization_utils_unittest.cc +++ b/components/metrics/serialization/serialization_utils_unittest.cc @@ -32,7 +32,7 @@ class SerializationUtilsTest : public testing::Test { void TestSerialization(MetricSample* sample) { std::string serialized(sample->ToString()); - ASSERT_EQ('\0', serialized[serialized.length() - 1]); + ASSERT_EQ('\0', serialized.back()); scoped_ptr deserialized = SerializationUtils::ParseSample(serialized); ASSERT_TRUE(deserialized); diff --git a/components/omnibox/browser/history_provider.cc b/components/omnibox/browser/history_provider.cc index 004d542..367d243 100644 --- a/components/omnibox/browser/history_provider.cc +++ b/components/omnibox/browser/history_provider.cc @@ -38,8 +38,7 @@ void HistoryProvider::DeleteMatch(const AutocompleteMatch& match) { bool HistoryProvider::PreventInlineAutocomplete( const AutocompleteInput& input) { return input.prevent_inline_autocomplete() || - (!input.text().empty() && - base::IsUnicodeWhitespace(input.text()[input.text().length() - 1])); + (!input.text().empty() && base::IsUnicodeWhitespace(input.text().back())); } HistoryProvider::HistoryProvider(AutocompleteProvider::Type type, diff --git a/components/search_engines/template_url_service.cc b/components/search_engines/template_url_service.cc index 40d2008..0e944ac 100644 --- a/components/search_engines/template_url_service.cc +++ b/components/search_engines/template_url_service.cc @@ -352,8 +352,8 @@ base::string16 TemplateURLService::CleanUserInputKeyword( result = url_formatter::StripWWW(result); // Remove trailing "/". - return (result.length() > 0 && result[result.length() - 1] == '/') ? - result.substr(0, result.length() - 1) : result; + return (result.empty() || result.back() != '/') ? + result : result.substr(0, result.length() - 1); } bool TemplateURLService::CanAddAutogeneratedKeyword( diff --git a/content/child/multipart_response_delegate.cc b/content/child/multipart_response_delegate.cc index 4e1841e..9e9bb9c 100644 --- a/content/child/multipart_response_delegate.cc +++ b/content/child/multipart_response_delegate.cc @@ -176,7 +176,7 @@ void MultipartResponseDelegate::OnReceivedData(const char* data, // If the last character is a new line character, go ahead and just send // everything we have buffered. This matches an optimization in Gecko. int send_length = data_.length() - boundary_.length(); - if (data_[data_.length() - 1] == '\n') + if (data_.back() == '\n') send_length = data_.length(); if (client_) client_->didReceiveData(loader_, diff --git a/extensions/common/permissions/socket_permission_entry.cc b/extensions/common/permissions/socket_permission_entry.cc index fdb6de8..a6fbb3b 100644 --- a/extensions/common/permissions/socket_permission_entry.cc +++ b/extensions/common/permissions/socket_permission_entry.cc @@ -31,8 +31,8 @@ const uint16_t kWildcardPortNumber = 0; const uint16_t kInvalidPort = 65535; bool StartsOrEndsWithWhitespace(const std::string& str) { - return !str.empty() && (base::IsUnicodeWhitespace(str[0]) || - base::IsUnicodeWhitespace(str[str.length() - 1])); + return !str.empty() && (base::IsUnicodeWhitespace(str.front()) || + base::IsUnicodeWhitespace(str.back())); } } // namespace diff --git a/gpu/command_buffer/client/program_info_manager_unittest.cc b/gpu/command_buffer/client/program_info_manager_unittest.cc index 1911b1c..64d896c 100644 --- a/gpu/command_buffer/client/program_info_manager_unittest.cc +++ b/gpu/command_buffer/client/program_info_manager_unittest.cc @@ -206,7 +206,7 @@ TEST_F(ProgramInfoManagerTest, UpdateES2) { EXPECT_LT(kNames[0].length(), static_cast(active_uniform_max_length)); EXPECT_EQ(kNames[ii], info->name); - EXPECT_EQ(kNames[ii][kNames[ii].length() - 1] == ']', info->is_array); + EXPECT_EQ(kNames[ii].back() == ']', info->is_array); EXPECT_EQ(data.uniforms[ii].size, static_cast(info->element_locations.size())); for (int32_t uu = 0; uu < data.uniforms[ii].size; ++uu) { diff --git a/media/filters/video_cadence_estimator_unittest.cc b/media/filters/video_cadence_estimator_unittest.cc index 939b35c..604a99b 100644 --- a/media/filters/video_cadence_estimator_unittest.cc +++ b/media/filters/video_cadence_estimator_unittest.cc @@ -27,8 +27,8 @@ static base::TimeDelta Interval(double hertz) { } std::vector CreateCadenceFromString(const std::string& cadence) { - CHECK_EQ('[', cadence[0]); - CHECK_EQ(']', cadence[cadence.length() - 1]); + CHECK_EQ('[', cadence.front()); + CHECK_EQ(']', cadence.back()); std::vector result; for (const std::string& token : diff --git a/net/base/filename_util_internal.cc b/net/base/filename_util_internal.cc index 2f038ad..2b06ee5 100644 --- a/net/base/filename_util_internal.cc +++ b/net/base/filename_util_internal.cc @@ -162,8 +162,8 @@ bool IsShellIntegratedExtension(const base::FilePath::StringType& extension) { // http://www.juniper.net/security/auto/vulnerabilities/vuln2612.html // Files become magical if they end in a CLSID, so block such extensions. if (!extension_lower.empty() && - (extension_lower[0] == FILE_PATH_LITERAL('{')) && - (extension_lower[extension_lower.length() - 1] == FILE_PATH_LITERAL('}'))) + (extension_lower.front() == FILE_PATH_LITERAL('{')) && + (extension_lower.back() == FILE_PATH_LITERAL('}'))) return true; return false; } diff --git a/net/cookies/canonical_cookie.cc b/net/cookies/canonical_cookie.cc index 34a55af..cdd011f 100644 --- a/net/cookies/canonical_cookie.cc +++ b/net/cookies/canonical_cookie.cc @@ -362,8 +362,7 @@ bool CanonicalCookie::IsOnPath(const std::string& url_path) const { // the cookie path ends in a trailing '/', or that we prefix up to a '/' // in the url path. Since we know that the url path length is greater // than the cookie path length, it's safe to index one byte past. - if (path_.length() != url_path.length() && - path_[path_.length() - 1] != '/' && + if (path_.length() != url_path.length() && path_.back() != '/' && url_path[path_.length()] != '/') return false; diff --git a/net/cookies/cookie_monster_unittest.cc b/net/cookies/cookie_monster_unittest.cc index db6b80f..f9d9aeb 100644 --- a/net/cookies/cookie_monster_unittest.cc +++ b/net/cookies/cookie_monster_unittest.cc @@ -388,7 +388,7 @@ class CookieMonsterTestBase : public CookieStoreTest { base::SPLIT_WANT_ALL)) { DCHECK(!token.empty()); // Take last character as priority. - CookiePriority priority = CharToPriority(token[token.length() - 1]); + CookiePriority priority = CharToPriority(token.back()); std::string priority_str = CookiePriorityToString(priority); // The rest of the string (possibly empty) specifies repetition. int rep = 1; diff --git a/net/ftp/ftp_directory_listing_parser_vms.cc b/net/ftp/ftp_directory_listing_parser_vms.cc index e05f2c7..8139ee0 100644 --- a/net/ftp/ftp_directory_listing_parser_vms.cc +++ b/net/ftp/ftp_directory_listing_parser_vms.cc @@ -113,7 +113,7 @@ bool LooksLikeVmsFileProtectionListingPart(const base::string16& input) { bool LooksLikeVmsFileProtectionListing(const base::string16& input) { if (input.length() < 2) return false; - if (input[0] != '(' || input[input.length() - 1] != ')') + if (input.front() != '(' || input.back() != ')') return false; // We expect four parts of the file protection listing: for System, Owner, @@ -133,7 +133,7 @@ bool LooksLikeVmsFileProtectionListing(const base::string16& input) { bool LooksLikeVmsUserIdentificationCode(const base::string16& input) { if (input.length() < 2) return false; - return input[0] == '[' && input[input.length() - 1] == ']'; + return input.front() == '[' && input.back() == ']'; } bool LooksLikeVMSError(const base::string16& text) { diff --git a/net/ftp/ftp_network_transaction.cc b/net/ftp/ftp_network_transaction.cc index 9c2c136..3b31bc1 100644 --- a/net/ftp/ftp_network_transaction.cc +++ b/net/ftp/ftp_network_transaction.cc @@ -485,7 +485,7 @@ std::string FtpNetworkTransaction::GetRequestPathForFtpCommand( } // Make sure that if the path is expected to be a file, it won't end // with a trailing slash. - if (!is_directory && path.length() > 1 && path[path.length() - 1] == '/') + if (!is_directory && path.length() > 1 && path.back() == '/') path.erase(path.length() - 1); UnescapeRule::Type unescape_rules = UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS; @@ -886,7 +886,7 @@ int FtpNetworkTransaction::ProcessResponsePWD(const FtpCtrlResponse& response) { } if (system_type_ == SYSTEM_TYPE_VMS) line = FtpUtil::VMSPathToUnix(line); - if (line.length() && line[line.length() - 1] == '/') + if (!line.empty() && line.back() == '/') line.erase(line.length() - 1); current_remote_directory_ = line; next_state_ = STATE_CTRL_WRITE_TYPE; diff --git a/net/ftp/ftp_util.cc b/net/ftp/ftp_util.cc index a2bee15..d7f759c 100644 --- a/net/ftp/ftp_util.cc +++ b/net/ftp/ftp_util.cc @@ -82,7 +82,7 @@ std::string FtpUtil::UnixDirectoryPathToVMS(const std::string& unix_path) { std::string path(unix_path); - if (path[path.length() - 1] != '/') + if (path.back() != '/') path.append("/"); // Reuse logic from UnixFilePathToVMS by appending a fake file name to the @@ -121,7 +121,7 @@ std::string FtpUtil::VMSPathToUnix(const std::string& vms_path) { std::replace(result.begin(), result.end(), ']', '/'); // Make sure the result doesn't end with a slash. - if (result.length() && result[result.length() - 1] == '/') + if (!result.empty() && result.back() == '/') result = result.substr(0, result.length() - 1); return result; diff --git a/net/proxy/proxy_config_service_linux.cc b/net/proxy/proxy_config_service_linux.cc index 139e308..314bf71 100644 --- a/net/proxy/proxy_config_service_linux.cc +++ b/net/proxy/proxy_config_service_linux.cc @@ -85,7 +85,7 @@ std::string FixupProxyHostScheme(ProxyServer::Scheme scheme, host = "socks5://" + host; // If there is a trailing slash, remove it so |host| will parse correctly // even if it includes a port number (since the slash is not numeric). - if (host.length() && host[host.length() - 1] == '/') + if (!host.empty() && host.back() == '/') host.resize(host.length() - 1); return host; } diff --git a/net/tools/balsa/balsa_frame.cc b/net/tools/balsa/balsa_frame.cc index 8505ac1..37256eb 100644 --- a/net/tools/balsa/balsa_frame.cc +++ b/net/tools/balsa/balsa_frame.cc @@ -751,9 +751,9 @@ void ProcessChunkExtensionsManual(base::StringPiece all_extensions, SplitStringPiece(extension, '=', &key, &value); if (!value.empty()) { // Strip quotation marks if they exist. - if (!value.empty() && value[0] == '"') + if (!value.empty() && value.front() == '"') value.remove_prefix(1); - if (!value.empty() && value[value.length() - 1] == '"') + if (!value.empty() && value.back() == '"') value.remove_suffix(1); } diff --git a/net/tools/flip_server/url_to_filename_encoder.h b/net/tools/flip_server/url_to_filename_encoder.h index 583e2fb..c68a9a1 100644 --- a/net/tools/flip_server/url_to_filename_encoder.h +++ b/net/tools/flip_server/url_to_filename_encoder.h @@ -108,7 +108,7 @@ class UrlToFilenameEncoder { #endif } else { std::string clean_url(url); - if (clean_url.length() && clean_url[clean_url.length() - 1] == '/') + if (clean_url.length() && clean_url.back() == '/') clean_url.append("index.html"); std::string host = UrlUtilities::GetUrlHost(clean_url); diff --git a/net/tools/quic/quic_in_memory_cache.cc b/net/tools/quic/quic_in_memory_cache.cc index 832419e..2dee018 100644 --- a/net/tools/quic/quic_in_memory_cache.cc +++ b/net/tools/quic/quic_in_memory_cache.cc @@ -172,7 +172,7 @@ void QuicInMemoryCache::InitializeFromDirectory(const string& cache_directory) { size_t path_start = base.find_first_of('/'); StringPiece host(StringPiece(base).substr(0, path_start)); StringPiece path(StringPiece(base).substr(path_start)); - if (path[path.length() - 1] == ',') { + if (path.back() == ',') { path.remove_suffix(1); } StringPiece body(file_contents.data() + headers_end, diff --git a/sandbox/win/src/win_utils.cc b/sandbox/win/src/win_utils.cc index 3717a97..ef4d803 100644 --- a/sandbox/win/src/win_utils.cc +++ b/sandbox/win/src/win_utils.cc @@ -226,7 +226,7 @@ bool SameObject(HANDLE handle, const wchar_t* full_path) { // This may end with a backslash. const wchar_t kBackslash = '\\'; - if (path[path.length() - 1] == kBackslash) + if (path.back() == kBackslash) path = path.substr(0, path.length() - 1); // Perfect match (case-insesitive check). diff --git a/storage/browser/fileapi/external_mount_points.cc b/storage/browser/fileapi/external_mount_points.cc index 9fdb4f9..f9ffdbd 100644 --- a/storage/browser/fileapi/external_mount_points.cc +++ b/storage/browser/fileapi/external_mount_points.cc @@ -24,7 +24,7 @@ base::FilePath NormalizeFilePath(const base::FilePath& path) { return path; base::FilePath::StringType path_str = path.StripTrailingSeparators().value(); - if (!base::FilePath::IsSeparator(path_str[path_str.length() - 1])) + if (!base::FilePath::IsSeparator(path_str.back())) path_str.append(FILE_PATH_LITERAL("/")); return base::FilePath(path_str).NormalizePathSeparators(); diff --git a/styleguide/c++/c++11.html b/styleguide/c++/c++11.html index cccae2e..239a282 100644 --- a/styleguide/c++/c++11.html +++ b/styleguide/c++/c++11.html @@ -438,6 +438,17 @@ std::end +String Direct Reference Functions +std::string::front() and std::string::back() +Returns a reference to the front or back of a string + +std::basic_string::front and + +std::basic_string::back +Discussion thread + + + Type Traits All C++11 features in <type_traits> except for aligned storage (see separate item), e.g.:
integral_constant
@@ -985,17 +996,6 @@ std:quick_exit -String Direct Reference Functions -std::string::front() and std::string::back() -Returns a reference to the front or back of a string - -std::basic_string::front and - -std::basic_string::back - - - - String to Number Functions std::stoi(), std::stol(), std::stoul(), std::stoll, std::stoull(), diff --git a/ui/gfx/text_elider.cc b/ui/gfx/text_elider.cc index 65e24e8..e8993d7 100644 --- a/ui/gfx/text_elider.cc +++ b/ui/gfx/text_elider.cc @@ -570,7 +570,7 @@ void RectangleText::AddString(const base::string16& input) { // The BREAK_NEWLINE iterator will keep the trailing newline character, // except in the case of the last line, which may not have one. Remove // the newline character, if it exists. - last_line_ended_in_lf_ = !line.empty() && line[line.length() - 1] == '\n'; + last_line_ended_in_lf_ = !line.empty() && line.back() == '\n'; if (last_line_ended_in_lf_) line.resize(line.length() - 1); AddLine(line); diff --git a/ui/shell_dialogs/select_file_dialog_win.cc b/ui/shell_dialogs/select_file_dialog_win.cc index fd48e1f..cbccd3a 100644 --- a/ui/shell_dialogs/select_file_dialog_win.cc +++ b/ui/shell_dialogs/select_file_dialog_win.cc @@ -684,7 +684,7 @@ std::wstring AppendExtensionIfNeeded( if (!(filter_selected.empty() || filter_selected == L"*.*") && !base::win::RegKey(HKEY_CLASSES_ROOT, key.c_str(), KEY_READ).Valid() && file_extension != suggested_ext) { - if (return_value[return_value.length() - 1] != L'.') + if (return_value.back() != L'.') return_value.append(L"."); return_value.append(suggested_ext); } -- cgit v1.1