From 18b746188c73a83ad57b99558914ba9421e8a20a Mon Sep 17 00:00:00 2001 From: Tomasz Wasilczyk Date: Thu, 10 Aug 2023 23:29:50 +0000 Subject: [PATCH] Migrate String8/16 from .string() to c_str() This will align usage with std::string. Bug: 295394788 Test: make checkbuild Change-Id: Ic5c215b011197950dcbcb0339f44cc68fefc65a1 --- fs_mgr/libfiemap/binder.cpp | 26 +++++++++++++------------- healthd/BatteryMonitor.cpp | 31 ++++++++++++------------------- healthd/BatteryMonitor_v1.cpp | 31 ++++++++++++------------------- libutils/ProcessCallStack.cpp | 5 ++--- libutils/RefBase.cpp | 2 +- libutils/String16_fuzz.cpp | 6 +++--- libutils/String8_test.cpp | 20 ++++++++++---------- libutils/Tokenizer.cpp | 10 +++++----- 8 files changed, 58 insertions(+), 73 deletions(-) diff --git a/fs_mgr/libfiemap/binder.cpp b/fs_mgr/libfiemap/binder.cpp index 41e534af4..439aac969 100644 --- a/fs_mgr/libfiemap/binder.cpp +++ b/fs_mgr/libfiemap/binder.cpp @@ -77,7 +77,7 @@ class ImageManagerBinder final : public IImageManager { static FiemapStatus ToFiemapStatus(const char* func, const binder::Status& status) { if (!status.isOk()) { - LOG(ERROR) << func << " binder returned: " << status.toString8().string(); + LOG(ERROR) << func << " binder returned: " << status.toString8().c_str(); if (status.serviceSpecificErrorCode() != 0) { return FiemapStatus::FromErrorCode(status.serviceSpecificErrorCode()); } else { @@ -106,7 +106,7 @@ bool ImageManagerBinder::DeleteBackingImage(const std::string& name) { auto status = manager_->deleteBackingImage(name); if (!status.isOk()) { LOG(ERROR) << __PRETTY_FUNCTION__ - << " binder returned: " << status.exceptionMessage().string(); + << " binder returned: " << status.exceptionMessage().c_str(); return false; } return true; @@ -122,7 +122,7 @@ bool ImageManagerBinder::MapImageDevice(const std::string& name, auto status = manager_->mapImageDevice(name, timeout_ms_count, &map); if (!status.isOk()) { LOG(ERROR) << __PRETTY_FUNCTION__ - << " binder returned: " << status.exceptionMessage().string(); + << " binder returned: " << status.exceptionMessage().c_str(); return false; } *path = map.path; @@ -133,7 +133,7 @@ bool ImageManagerBinder::UnmapImageDevice(const std::string& name) { auto status = manager_->unmapImageDevice(name); if (!status.isOk()) { LOG(ERROR) << __PRETTY_FUNCTION__ - << " binder returned: " << status.exceptionMessage().string(); + << " binder returned: " << status.exceptionMessage().c_str(); return false; } return true; @@ -144,7 +144,7 @@ bool ImageManagerBinder::BackingImageExists(const std::string& name) { auto status = manager_->backingImageExists(name, &retval); if (!status.isOk()) { LOG(ERROR) << __PRETTY_FUNCTION__ - << " binder returned: " << status.exceptionMessage().string(); + << " binder returned: " << status.exceptionMessage().c_str(); return false; } return retval; @@ -155,7 +155,7 @@ bool ImageManagerBinder::IsImageMapped(const std::string& name) { auto status = manager_->isImageMapped(name, &retval); if (!status.isOk()) { LOG(ERROR) << __PRETTY_FUNCTION__ - << " binder returned: " << status.exceptionMessage().string(); + << " binder returned: " << status.exceptionMessage().c_str(); return false; } return retval; @@ -175,7 +175,7 @@ std::vector ImageManagerBinder::GetAllBackingImages() { auto status = manager_->getAllBackingImages(&retval); if (!status.isOk()) { LOG(ERROR) << __PRETTY_FUNCTION__ - << " binder returned: " << status.exceptionMessage().string(); + << " binder returned: " << status.exceptionMessage().c_str(); } return retval; } @@ -189,7 +189,7 @@ bool ImageManagerBinder::RemoveAllImages() { auto status = manager_->removeAllImages(); if (!status.isOk()) { LOG(ERROR) << __PRETTY_FUNCTION__ - << " binder returned: " << status.exceptionMessage().string(); + << " binder returned: " << status.exceptionMessage().c_str(); return false; } return true; @@ -199,7 +199,7 @@ bool ImageManagerBinder::DisableImage(const std::string& name) { auto status = manager_->disableImage(name); if (!status.isOk()) { LOG(ERROR) << __PRETTY_FUNCTION__ - << " binder returned: " << status.exceptionMessage().string(); + << " binder returned: " << status.exceptionMessage().c_str(); return false; } return true; @@ -209,7 +209,7 @@ bool ImageManagerBinder::RemoveDisabledImages() { auto status = manager_->removeDisabledImages(); if (!status.isOk()) { LOG(ERROR) << __PRETTY_FUNCTION__ - << " binder returned: " << status.exceptionMessage().string(); + << " binder returned: " << status.exceptionMessage().c_str(); return false; } return true; @@ -219,7 +219,7 @@ bool ImageManagerBinder::GetMappedImageDevice(const std::string& name, std::stri auto status = manager_->getMappedImageDevice(name, device); if (!status.isOk()) { LOG(ERROR) << __PRETTY_FUNCTION__ - << " binder returned: " << status.exceptionMessage().string(); + << " binder returned: " << status.exceptionMessage().c_str(); return false; } return !device->empty(); @@ -230,7 +230,7 @@ bool ImageManagerBinder::IsImageDisabled(const std::string& name) { auto status = manager_->isImageDisabled(name, &retval); if (!status.isOk()) { LOG(ERROR) << __PRETTY_FUNCTION__ - << " binder returned: " << status.exceptionMessage().string(); + << " binder returned: " << status.exceptionMessage().c_str(); return false; } return retval; @@ -249,7 +249,7 @@ std::unique_ptr IImageManager::Open(const std::string& dir, auto status = service->openImageService(dir, &manager); if (!status.isOk() || !manager) { - LOG(ERROR) << "Could not acquire IImageManager: " << status.exceptionMessage().string(); + LOG(ERROR) << "Could not acquire IImageManager: " << status.exceptionMessage().c_str(); return nullptr; } return std::make_unique(std::move(service), std::move(manager)); diff --git a/healthd/BatteryMonitor.cpp b/healthd/BatteryMonitor.cpp index bd7955a7f..15c30be41 100644 --- a/healthd/BatteryMonitor.cpp +++ b/healthd/BatteryMonitor.cpp @@ -444,12 +444,10 @@ void BatteryMonitor::updateValues(void) { for (size_t i = 0; i < mChargerNames.size(); i++) { String8 path; - path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, - mChargerNames[i].string()); + path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, mChargerNames[i].c_str()); if (getIntField(path)) { path.clear(); - path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, - mChargerNames[i].string()); + path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, mChargerNames[i].c_str()); switch(readPowerSupplyType(path)) { case ANDROID_POWER_SUPPLY_TYPE_AC: mHealthInfo->chargerAcOnline = true; @@ -466,26 +464,24 @@ void BatteryMonitor::updateValues(void) { default: path.clear(); path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH, - mChargerNames[i].string()); - if (access(path.string(), R_OK) == 0) + mChargerNames[i].c_str()); + if (access(path.c_str(), R_OK) == 0) mHealthInfo->chargerDockOnline = true; else KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n", - mChargerNames[i].string()); + mChargerNames[i].c_str()); } path.clear(); path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH, - mChargerNames[i].string()); - int ChargingCurrent = - (access(path.string(), R_OK) == 0) ? getIntField(path) : 0; + mChargerNames[i].c_str()); + int ChargingCurrent = (access(path.c_str(), R_OK) == 0) ? getIntField(path) : 0; path.clear(); path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH, - mChargerNames[i].string()); + mChargerNames[i].c_str()); int ChargingVoltage = - (access(path.string(), R_OK) == 0) ? getIntField(path) : - DEFAULT_VBUS_VOLTAGE; + (access(path.c_str(), R_OK) == 0) ? getIntField(path) : DEFAULT_VBUS_VOLTAGE; double power = ((double)ChargingCurrent / MILLION) * ((double)ChargingVoltage / MILLION); @@ -775,8 +771,7 @@ void BatteryMonitor::init(struct healthd_config *hc) { case ANDROID_POWER_SUPPLY_TYPE_DOCK: path.clear(); path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name); - if (access(path.string(), R_OK) == 0) - mChargerNames.add(String8(name)); + if (access(path.c_str(), R_OK) == 0) mChargerNames.add(String8(name)); break; case ANDROID_POWER_SUPPLY_TYPE_BATTERY: @@ -958,12 +953,10 @@ void BatteryMonitor::init(struct healthd_config *hc) { // Look for "is_dock" file path.clear(); path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH, name); - if (access(path.string(), R_OK) == 0) { + if (access(path.c_str(), R_OK) == 0) { path.clear(); path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name); - if (access(path.string(), R_OK) == 0) - mChargerNames.add(String8(name)); - + if (access(path.c_str(), R_OK) == 0) mChargerNames.add(String8(name)); } } } diff --git a/healthd/BatteryMonitor_v1.cpp b/healthd/BatteryMonitor_v1.cpp index b87c493b2..b2d6518f7 100644 --- a/healthd/BatteryMonitor_v1.cpp +++ b/healthd/BatteryMonitor_v1.cpp @@ -358,12 +358,10 @@ void BatteryMonitor::updateValues(void) { for (size_t i = 0; i < mChargerNames.size(); i++) { String8 path; - path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, - mChargerNames[i].string()); + path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, mChargerNames[i].c_str()); if (getIntField(path)) { path.clear(); - path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, - mChargerNames[i].string()); + path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, mChargerNames[i].c_str()); switch(readPowerSupplyType(path)) { case ANDROID_POWER_SUPPLY_TYPE_AC: mHealthInfo->chargerAcOnline = true; @@ -380,26 +378,24 @@ void BatteryMonitor::updateValues(void) { default: path.clear(); path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH, - mChargerNames[i].string()); - if (access(path.string(), R_OK) == 0) + mChargerNames[i].c_str()); + if (access(path.c_str(), R_OK) == 0) mHealthInfo->chargerDockOnline = true; else KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n", - mChargerNames[i].string()); + mChargerNames[i].c_str()); } path.clear(); path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH, - mChargerNames[i].string()); - int ChargingCurrent = - (access(path.string(), R_OK) == 0) ? getIntField(path) : 0; + mChargerNames[i].c_str()); + int ChargingCurrent = (access(path.c_str(), R_OK) == 0) ? getIntField(path) : 0; path.clear(); path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH, - mChargerNames[i].string()); + mChargerNames[i].c_str()); int ChargingVoltage = - (access(path.string(), R_OK) == 0) ? getIntField(path) : - DEFAULT_VBUS_VOLTAGE; + (access(path.c_str(), R_OK) == 0) ? getIntField(path) : DEFAULT_VBUS_VOLTAGE; double power = ((double)ChargingCurrent / MILLION) * ((double)ChargingVoltage / MILLION); @@ -628,8 +624,7 @@ void BatteryMonitor::init(struct healthd_config *hc) { case ANDROID_POWER_SUPPLY_TYPE_DOCK: path.clear(); path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name); - if (access(path.string(), R_OK) == 0) - mChargerNames.add(String8(name)); + if (access(path.c_str(), R_OK) == 0) mChargerNames.add(String8(name)); break; case ANDROID_POWER_SUPPLY_TYPE_BATTERY: @@ -767,12 +762,10 @@ void BatteryMonitor::init(struct healthd_config *hc) { // Look for "is_dock" file path.clear(); path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH, name); - if (access(path.string(), R_OK) == 0) { + if (access(path.c_str(), R_OK) == 0) { path.clear(); path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name); - if (access(path.string(), R_OK) == 0) - mChargerNames.add(String8(name)); - + if (access(path.c_str(), R_OK) == 0) mChargerNames.add(String8(name)); } } } diff --git a/libutils/ProcessCallStack.cpp b/libutils/ProcessCallStack.cpp index f054de98b..4b27bdddd 100644 --- a/libutils/ProcessCallStack.cpp +++ b/libutils/ProcessCallStack.cpp @@ -205,8 +205,7 @@ void ProcessCallStack::print(Printer& printer) const { } void ProcessCallStack::printInternal(Printer& printer, Printer& csPrinter) const { - dumpProcessHeader(printer, getpid(), - getTimeString(mTimeUpdated).string()); + dumpProcessHeader(printer, getpid(), getTimeString(mTimeUpdated).c_str()); for (size_t i = 0; i < mThreadMap.size(); ++i) { pid_t tid = mThreadMap.keyAt(i); @@ -214,7 +213,7 @@ void ProcessCallStack::printInternal(Printer& printer, Printer& csPrinter) const const String8& threadName = threadInfo.threadName; printer.printLine(""); - printer.printFormatLine("\"%s\" sysTid=%d", threadName.string(), tid); + printer.printFormatLine("\"%s\" sysTid=%d", threadName.c_str(), tid); threadInfo.callStack.print(csPrinter); } diff --git a/libutils/RefBase.cpp b/libutils/RefBase.cpp index ab122c732..e0a2846d4 100644 --- a/libutils/RefBase.cpp +++ b/libutils/RefBase.cpp @@ -330,7 +330,7 @@ public: this); int rc = open(name, O_RDWR | O_CREAT | O_APPEND, 0644); if (rc >= 0) { - (void)write(rc, text.string(), text.length()); + (void)write(rc, text.c_str(), text.length()); close(rc); ALOGI("STACK TRACE for %p saved in %s", this, name); } diff --git a/libutils/String16_fuzz.cpp b/libutils/String16_fuzz.cpp index d7e5ec783..a271aeebd 100644 --- a/libutils/String16_fuzz.cpp +++ b/libutils/String16_fuzz.cpp @@ -25,7 +25,7 @@ std::vector void { - str1.string(); + str1.c_str(); }), ([](FuzzedDataProvider&, android::String16 str1, android::String16) -> void { str1.isStaticString(); @@ -39,7 +39,7 @@ std::vector void { - str1.contains(str2.string()); + str1.contains(str2.c_str()); }), ([](FuzzedDataProvider&, android::String16 str1, android::String16 str2) -> void { str1.compare(str2); @@ -52,7 +52,7 @@ std::vector void { int pos = dataProvider.ConsumeIntegralInRange(0, str1.size()); - str1.insert(pos, str2.string()); + str1.insert(pos, str2.c_str()); }), // Find and replace operations diff --git a/libutils/String8_test.cpp b/libutils/String8_test.cpp index 35fd512f4..9c12cb12d 100644 --- a/libutils/String8_test.cpp +++ b/libutils/String8_test.cpp @@ -36,7 +36,7 @@ protected: TEST_F(String8Test, Cstr) { String8 tmp("Hello, world!"); - EXPECT_STREQ(tmp.string(), "Hello, world!"); + EXPECT_STREQ(tmp.c_str(), "Hello, world!"); } TEST_F(String8Test, OperatorPlus) { @@ -45,16 +45,16 @@ TEST_F(String8Test, OperatorPlus) { // Test adding String8 + const char* const char* ccsrc2 = "world!"; String8 dst1 = src1 + ccsrc2; - EXPECT_STREQ(dst1.string(), "Hello, world!"); - EXPECT_STREQ(src1.string(), "Hello, "); + EXPECT_STREQ(dst1.c_str(), "Hello, world!"); + EXPECT_STREQ(src1.c_str(), "Hello, "); EXPECT_STREQ(ccsrc2, "world!"); // Test adding String8 + String8 String8 ssrc2("world!"); String8 dst2 = src1 + ssrc2; - EXPECT_STREQ(dst2.string(), "Hello, world!"); - EXPECT_STREQ(src1.string(), "Hello, "); - EXPECT_STREQ(ssrc2.string(), "world!"); + EXPECT_STREQ(dst2.c_str(), "Hello, world!"); + EXPECT_STREQ(src1.c_str(), "Hello, "); + EXPECT_STREQ(ssrc2.c_str(), "world!"); } TEST_F(String8Test, OperatorPlusEquals) { @@ -63,14 +63,14 @@ TEST_F(String8Test, OperatorPlusEquals) { // Testing String8 += String8 String8 src2(" is my passport."); src1 += src2; - EXPECT_STREQ(src1.string(), "My voice is my passport."); - EXPECT_STREQ(src2.string(), " is my passport."); + EXPECT_STREQ(src1.c_str(), "My voice is my passport."); + EXPECT_STREQ(src2.c_str(), " is my passport."); // Adding const char* to the previous string. const char* src3 = " Verify me."; src1 += src3; - EXPECT_STREQ(src1.string(), "My voice is my passport. Verify me."); - EXPECT_STREQ(src2.string(), " is my passport."); + EXPECT_STREQ(src1.c_str(), "My voice is my passport. Verify me."); + EXPECT_STREQ(src2.c_str(), " is my passport."); EXPECT_STREQ(src3, " Verify me."); } diff --git a/libutils/Tokenizer.cpp b/libutils/Tokenizer.cpp index c3ec1652b..9fc955c1a 100644 --- a/libutils/Tokenizer.cpp +++ b/libutils/Tokenizer.cpp @@ -50,15 +50,15 @@ status_t Tokenizer::open(const String8& filename, Tokenizer** outTokenizer) { *outTokenizer = nullptr; int result = OK; - int fd = ::open(filename.string(), O_RDONLY); + int fd = ::open(filename.c_str(), O_RDONLY); if (fd < 0) { result = -errno; - ALOGE("Error opening file '%s': %s", filename.string(), strerror(errno)); + ALOGE("Error opening file '%s': %s", filename.c_str(), strerror(errno)); } else { struct stat stat; if (fstat(fd, &stat)) { result = -errno; - ALOGE("Error getting size of file '%s': %s", filename.string(), strerror(errno)); + ALOGE("Error getting size of file '%s': %s", filename.c_str(), strerror(errno)); } else { size_t length = size_t(stat.st_size); @@ -80,7 +80,7 @@ status_t Tokenizer::open(const String8& filename, Tokenizer** outTokenizer) { ssize_t nrd = read(fd, buffer, length); if (nrd < 0) { result = -errno; - ALOGE("Error reading file '%s': %s", filename.string(), strerror(errno)); + ALOGE("Error reading file '%s': %s", filename.c_str(), strerror(errno)); delete[] buffer; buffer = nullptr; } else { @@ -106,7 +106,7 @@ status_t Tokenizer::fromContents(const String8& filename, String8 Tokenizer::getLocation() const { String8 result; - result.appendFormat("%s:%d", mFilename.string(), mLineNumber); + result.appendFormat("%s:%d", mFilename.c_str(), mLineNumber); return result; }