Skip to content

Commit

Permalink
Move string tokenizer to base/strings.
Browse files Browse the repository at this point in the history
BUG=

Review URL: https://codereview.chromium.org/12087091

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@180211 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
brettw@chromium.org committed Feb 2, 2013
1 parent 11e45ef commit f4ebe77
Show file tree
Hide file tree
Showing 66 changed files with 190 additions and 190 deletions.
2 changes: 1 addition & 1 deletion base/base.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,9 @@
'string_number_conversions_unittest.cc',
'string_piece_unittest.cc',
'string_split_unittest.cc',
'string_tokenizer_unittest.cc',
'string_util_unittest.cc',
'stringprintf_unittest.cc',
'strings/string_tokenizer_unittest.cc',
'strings/stringize_macros_unittest.cc',
'synchronization/cancellation_flag_unittest.cc',
'synchronization/condition_variable_unittest.cc',
Expand Down
2 changes: 1 addition & 1 deletion base/base.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,6 @@
'string_piece.h',
'string_split.cc',
'string_split.h',
'string_tokenizer.h',
'string_util.cc',
'string_util.h',
'string_util_posix.h',
Expand All @@ -399,6 +398,7 @@
'string16.h',
'stringprintf.cc',
'stringprintf.h',
'strings/string_tokenizer.h',
'strings/stringize_macros.h',
'supports_user_data.cc',
'supports_user_data.h',
Expand Down
18 changes: 9 additions & 9 deletions base/debug/trace_event_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
#include "base/memory/singleton.h"
#include "base/process_util.h"
#include "base/stl_util.h"
#include "base/stringprintf.h"
#include "base/string_tokenizer.h"
#include "base/string_util.h"
#include "base/stringprintf.h"
#include "base/strings/string_tokenizer.h"
#include "base/sys_info.h"
#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
#include "base/threading/platform_thread.h"
Expand Down Expand Up @@ -162,7 +162,7 @@ TraceEvent::TraceEvent(int thread_id,
}

if (alloc_size) {
parameter_copy_storage_ = new base::RefCountedString;
parameter_copy_storage_ = new RefCountedString;
parameter_copy_storage_->data().resize(alloc_size);
char* ptr = string_as_array(&parameter_copy_storage_->data());
const char* end = ptr + alloc_size;
Expand Down Expand Up @@ -279,7 +279,7 @@ void TraceEvent::AppendAsJSON(std::string* out) const {

TraceResultBuffer::OutputCallback
TraceResultBuffer::SimpleOutput::GetCallback() {
return base::Bind(&SimpleOutput::Append, base::Unretained(this));
return Bind(&SimpleOutput::Append, Unretained(this));
}

void TraceResultBuffer::SimpleOutput::Append(
Expand Down Expand Up @@ -365,7 +365,7 @@ TraceLog::TraceLog()
#if defined(OS_NACL) // NaCl shouldn't expose the process id.
SetProcessID(0);
#else
SetProcessID(static_cast<int>(base::GetCurrentProcId()));
SetProcessID(static_cast<int>(GetCurrentProcId()));
#endif
}

Expand Down Expand Up @@ -442,7 +442,7 @@ const unsigned char* TraceLog::GetCategoryEnabledInternal(const char* name) {
// Don't hold on to the name pointer, so that we can create categories
// with strings not known at compile time (this is required by
// SetWatchEvent).
const char* new_name = base::strdup(name);
const char* new_name = strdup(name);
ANNOTATE_LEAKING_OBJECT_PTR(new_name);
g_categories[new_index] = new_name;
DCHECK(!g_category_enabled[new_index]);
Expand Down Expand Up @@ -657,15 +657,15 @@ void TraceLog::AddTraceEvent(char phase,
if (new_name != g_current_thread_name.Get().Get() &&
new_name && *new_name) {
g_current_thread_name.Get().Set(new_name);
base::hash_map<int, std::string>::iterator existing_name =
hash_map<int, std::string>::iterator existing_name =
thread_names_.find(thread_id);
if (existing_name == thread_names_.end()) {
// This is a new thread id, and a new name.
thread_names_[thread_id] = new_name;
} else {
// This is a thread id that we've seen before, but potentially with a
// new name.
std::vector<base::StringPiece> existing_names;
std::vector<StringPiece> existing_names;
Tokenize(existing_name->second, ",", &existing_names);
bool found = std::find(existing_names.begin(),
existing_names.end(),
Expand Down Expand Up @@ -756,7 +756,7 @@ void TraceLog::CancelWatchEvent() {

void TraceLog::AddThreadNameMetadataEvents() {
lock_.AssertAcquired();
for(base::hash_map<int, std::string>::iterator it = thread_names_.begin();
for(hash_map<int, std::string>::iterator it = thread_names_.begin();
it != thread_names_.end();
it++) {
if (!it->second.empty()) {
Expand Down
76 changes: 38 additions & 38 deletions base/process_util_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
#include "base/logging.h"
#include "base/string_number_conversions.h"
#include "base/string_split.h"
#include "base/string_tokenizer.h"
#include "base/string_util.h"
#include "base/strings/string_tokenizer.h"
#include "base/sys_info.h"
#include "base/threading/thread_restrictions.h"

namespace base {

namespace {

enum ParsingState {
Expand All @@ -31,7 +33,7 @@ const char kStatFile[] = "stat";

// Returns a FilePath to "/proc/pid".
FilePath GetProcPidDir(pid_t pid) {
return FilePath(kProcDir).Append(base::IntToString(pid));
return FilePath(kProcDir).Append(IntToString(pid));
}

// Fields from /proc/<pid>/stat, 0-based. See man 5 proc.
Expand All @@ -53,7 +55,7 @@ enum ProcStatsFields {
bool ReadProcStats(pid_t pid, std::string* buffer) {
buffer->clear();
// Synchronously reading files in /proc is safe.
base::ThreadRestrictions::ScopedAllowIO allow_io;
ThreadRestrictions::ScopedAllowIO allow_io;

FilePath stat_file = GetProcPidDir(pid).Append(kStatFile);
if (!file_util::ReadFileToString(stat_file, buffer)) {
Expand Down Expand Up @@ -98,7 +100,7 @@ bool ParseProcStats(const std::string& stats_data,

// Split the rest.
std::vector<std::string> other_stats;
base::SplitString(stats_data.substr(close_parens_idx + 2), ' ', &other_stats);
SplitString(stats_data.substr(close_parens_idx + 2), ' ', &other_stats);
for (size_t i = 0; i < other_stats.size(); ++i)
proc_stats->push_back(other_stats[i]);
return true;
Expand All @@ -113,7 +115,7 @@ int GetProcStatsFieldAsInt(const std::vector<std::string>& proc_stats,
CHECK_LT(static_cast<size_t>(field_num), proc_stats.size());

int value;
return base::StringToInt(proc_stats[field_num], &value) ? value : 0;
return StringToInt(proc_stats[field_num], &value) ? value : 0;
}

// Same as GetProcStatsFieldAsInt(), but for size_t values.
Expand All @@ -123,7 +125,7 @@ size_t GetProcStatsFieldAsSizeT(const std::vector<std::string>& proc_stats,
CHECK_LT(static_cast<size_t>(field_num), proc_stats.size());

size_t value;
return base::StringToSizeT(proc_stats[field_num], &value) ? value : 0;
return StringToSizeT(proc_stats[field_num], &value) ? value : 0;
}

// Convenience wrapper around GetProcStatsFieldAsInt(), ParseProcStats() and
Expand Down Expand Up @@ -175,7 +177,7 @@ std::string GetProcStatsFieldAsString(
// delimiter.
bool GetProcCmdline(pid_t pid, std::vector<std::string>* proc_cmd_line_args) {
// Synchronously reading files in /proc is safe.
base::ThreadRestrictions::ScopedAllowIO allow_io;
ThreadRestrictions::ScopedAllowIO allow_io;

FilePath cmd_line_file = GetProcPidDir(pid).Append("cmdline");
std::string cmd_line;
Expand Down Expand Up @@ -204,7 +206,7 @@ pid_t ProcDirSlotToPid(const char* d_name) {
// Read the process's command line.
pid_t pid;
std::string pid_string(d_name);
if (!base::StringToInt(pid_string, &pid)) {
if (!StringToInt(pid_string, &pid)) {
NOTREACHED();
return 0;
}
Expand All @@ -230,12 +232,12 @@ int GetProcessCPU(pid_t pid) {
continue;

// Synchronously reading files in /proc is safe.
base::ThreadRestrictions::ScopedAllowIO allow_io;
ThreadRestrictions::ScopedAllowIO allow_io;

std::string stat;
FilePath stat_path = task_path.Append(ent->d_name).Append(kStatFile);
if (file_util::ReadFileToString(stat_path, &stat)) {
int cpu = base::ParseProcStatCPU(stat);
int cpu = ParseProcStatCPU(stat);
if (cpu > 0)
total_cpu += cpu;
}
Expand All @@ -252,14 +254,14 @@ size_t ReadProcStatusAndGetFieldAsSizeT(pid_t pid, const std::string& field) {
std::string status;
{
// Synchronously reading files in /proc is safe.
base::ThreadRestrictions::ScopedAllowIO allow_io;
ThreadRestrictions::ScopedAllowIO allow_io;
if (!file_util::ReadFileToString(stat_file, &status))
return 0;
}

StringTokenizer tokenizer(status, ":\n");
ParsingState state = KEY_NAME;
base::StringPiece last_key_name;
StringPiece last_key_name;
while (tokenizer.GetNext()) {
switch (state) {
case KEY_NAME:
Expand All @@ -274,13 +276,13 @@ size_t ReadProcStatusAndGetFieldAsSizeT(pid_t pid, const std::string& field) {
std::string value_str_trimmed;
TrimWhitespaceASCII(value_str, TRIM_ALL, &value_str_trimmed);
std::vector<std::string> split_value_str;
base::SplitString(value_str_trimmed, ' ', &split_value_str);
SplitString(value_str_trimmed, ' ', &split_value_str);
if (split_value_str.size() != 2 || split_value_str[1] != "kB") {
NOTREACHED();
return 0;
}
size_t value;
if (!base::StringToSizeT(split_value_str[0], &value)) {
if (!StringToSizeT(split_value_str[0], &value)) {
NOTREACHED();
return 0;
}
Expand All @@ -296,8 +298,6 @@ size_t ReadProcStatusAndGetFieldAsSizeT(pid_t pid, const std::string& field) {

} // namespace

namespace base {

#if defined(USE_LINUX_BREAKPAD)
size_t g_oom_size = 0U;
#endif
Expand Down Expand Up @@ -460,20 +460,20 @@ bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const {
{
FilePath statm_file = GetProcPidDir(process_).Append("statm");
// Synchronously reading files in /proc is safe.
base::ThreadRestrictions::ScopedAllowIO allow_io;
ThreadRestrictions::ScopedAllowIO allow_io;
bool ret = file_util::ReadFileToString(statm_file, &statm);
if (!ret || statm.length() == 0)
return false;
}

std::vector<std::string> statm_vec;
base::SplitString(statm, ' ', &statm_vec);
SplitString(statm, ' ', &statm_vec);
if (statm_vec.size() != 7)
return false; // Not the format we expect.

int statm_rss, statm_shared;
base::StringToInt(statm_vec[1], &statm_rss);
base::StringToInt(statm_vec[2], &statm_shared);
StringToInt(statm_vec[1], &statm_rss);
StringToInt(statm_vec[2], &statm_shared);

ws_usage->priv = (statm_rss - statm_shared) * page_size_kb;
ws_usage->shared = statm_shared * page_size_kb;
Expand Down Expand Up @@ -531,7 +531,7 @@ double ProcessMetrics::GetCPUUsage() {
// in your kernel configuration.
bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const {
// Synchronously reading files in /proc is safe.
base::ThreadRestrictions::ScopedAllowIO allow_io;
ThreadRestrictions::ScopedAllowIO allow_io;

std::string proc_io_contents;
FilePath io_file = GetProcPidDir(process_).Append("io");
Expand All @@ -553,16 +553,16 @@ bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const {
case KEY_VALUE:
DCHECK(!last_key_name.empty());
if (last_key_name == "syscr") {
base::StringToInt64(tokenizer.token_piece(),
StringToInt64(tokenizer.token_piece(),
reinterpret_cast<int64*>(&(*io_counters).ReadOperationCount));
} else if (last_key_name == "syscw") {
base::StringToInt64(tokenizer.token_piece(),
StringToInt64(tokenizer.token_piece(),
reinterpret_cast<int64*>(&(*io_counters).WriteOperationCount));
} else if (last_key_name == "rchar") {
base::StringToInt64(tokenizer.token_piece(),
StringToInt64(tokenizer.token_piece(),
reinterpret_cast<int64*>(&(*io_counters).ReadTransferCount));
} else if (last_key_name == "wchar") {
base::StringToInt64(tokenizer.token_piece(),
StringToInt64(tokenizer.token_piece(),
reinterpret_cast<int64*>(&(*io_counters).WriteTransferCount));
}
state = KEY_NAME;
Expand All @@ -577,7 +577,7 @@ ProcessMetrics::ProcessMetrics(ProcessHandle process)
last_time_(0),
last_system_time_(0),
last_cpu_(0) {
processor_count_ = base::SysInfo::NumberOfProcessors();
processor_count_ = SysInfo::NumberOfProcessors();
}


Expand Down Expand Up @@ -630,7 +630,7 @@ SystemMemoryInfoKB::SystemMemoryInfoKB()

bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) {
// Synchronously reading files in /proc is safe.
base::ThreadRestrictions::ScopedAllowIO allow_io;
ThreadRestrictions::ScopedAllowIO allow_io;

// Used memory is: total - free - buffers - caches
FilePath meminfo_file("/proc/meminfo");
Expand All @@ -657,15 +657,15 @@ bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) {
DCHECK_EQ(meminfo_fields[kMemActiveFileIndex-1], "Active(file):");
DCHECK_EQ(meminfo_fields[kMemInactiveFileIndex-1], "Inactive(file):");

base::StringToInt(meminfo_fields[kMemTotalIndex], &meminfo->total);
base::StringToInt(meminfo_fields[kMemFreeIndex], &meminfo->free);
base::StringToInt(meminfo_fields[kMemBuffersIndex], &meminfo->buffers);
base::StringToInt(meminfo_fields[kMemCachedIndex], &meminfo->cached);
base::StringToInt(meminfo_fields[kMemActiveAnonIndex], &meminfo->active_anon);
base::StringToInt(meminfo_fields[kMemInactiveAnonIndex],
StringToInt(meminfo_fields[kMemTotalIndex], &meminfo->total);
StringToInt(meminfo_fields[kMemFreeIndex], &meminfo->free);
StringToInt(meminfo_fields[kMemBuffersIndex], &meminfo->buffers);
StringToInt(meminfo_fields[kMemCachedIndex], &meminfo->cached);
StringToInt(meminfo_fields[kMemActiveAnonIndex], &meminfo->active_anon);
StringToInt(meminfo_fields[kMemInactiveAnonIndex],
&meminfo->inactive_anon);
base::StringToInt(meminfo_fields[kMemActiveFileIndex], &meminfo->active_file);
base::StringToInt(meminfo_fields[kMemInactiveFileIndex],
StringToInt(meminfo_fields[kMemActiveFileIndex], &meminfo->active_file);
StringToInt(meminfo_fields[kMemInactiveFileIndex],
&meminfo->inactive_file);
#if defined(OS_CHROMEOS)
// Chrome OS has a tweaked kernel that allows us to query Shmem, which is
Expand All @@ -674,7 +674,7 @@ bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) {
// string. It always appears after "Cached:".
for (size_t i = kMemCachedIndex+2; i < meminfo_fields.size(); i += 3) {
if (meminfo_fields[i] == "Shmem:") {
base::StringToInt(meminfo_fields[i+1], &meminfo->shmem);
StringToInt(meminfo_fields[i+1], &meminfo->shmem);
break;
}
}
Expand Down Expand Up @@ -854,7 +854,7 @@ bool AdjustOOMScore(ProcessId process, int score) {
// Attempt to write the newer oom_score_adj file first.
FilePath oom_file = oom_path.AppendASCII("oom_score_adj");
if (file_util::PathExists(oom_file)) {
std::string score_str = base::IntToString(score);
std::string score_str = IntToString(score);
DVLOG(1) << "Adjusting oom_score_adj of " << process << " to "
<< score_str;
int score_len = static_cast<int>(score_str.length());
Expand All @@ -872,7 +872,7 @@ bool AdjustOOMScore(ProcessId process, int score) {
const int kMaxOldOomScore = 15;

int converted_score = score * kMaxOldOomScore / kMaxOomScore;
std::string score_str = base::IntToString(converted_score);
std::string score_str = IntToString(converted_score);
DVLOG(1) << "Adjusting oom_adj of " << process << " to " << score_str;
int score_len = static_cast<int>(score_str.length());
return (score_len == file_util::WriteFile(oom_file,
Expand Down
10 changes: 7 additions & 3 deletions base/string_tokenizer.h → base/strings/string_tokenizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_STRING_TOKENIZER_H_
#define BASE_STRING_TOKENIZER_H_
#ifndef BASE_STRINGS_STRING_TOKENIZER_H_
#define BASE_STRINGS_STRING_TOKENIZER_H_

#include <algorithm>
#include <string>

#include "base/string_piece.h"

namespace base {

// StringTokenizerT is a simple string tokenizer class. It works like an
// iterator that with each step (see the Advance method) updates members that
// refer to the next token in the input string. The user may optionally
Expand Down Expand Up @@ -253,4 +255,6 @@ typedef StringTokenizerT<std::wstring, std::wstring::const_iterator>
WStringTokenizer;
typedef StringTokenizerT<std::string, const char*> CStringTokenizer;

#endif // BASE_STRING_TOKENIZER_H_
} // namespace base

#endif // BASE_STRINGS_STRING_TOKENIZER_H_
Loading

0 comments on commit f4ebe77

Please sign in to comment.