summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
Diffstat (limited to 'base')
-rw-r--r--base/file_util.h4
-rw-r--r--base/file_util_posix.cc7
-rw-r--r--base/file_util_win.cc10
-rw-r--r--base/string_util.cc36
-rw-r--r--base/string_util.h8
-rw-r--r--base/string_util_unittest.cc26
6 files changed, 89 insertions, 2 deletions
diff --git a/base/file_util.h b/base/file_util.h
index 9624e60..150b629 100644
--- a/base/file_util.h
+++ b/base/file_util.h
@@ -224,6 +224,10 @@ bool GetTempDir(std::wstring* path);
// TODO(erikkay): rename this function and track down all of the callers.
bool CreateTemporaryFileName(std::wstring* temp_file);
+// Same as CreateTemporaryFileName but the file is created in |dir|.
+bool CreateTemporaryFileNameInDir(const std::wstring& dir,
+ std::wstring* temp_file);
+
// Create a new directory under TempPath. If prefix is provided, the new
// directory name is in the format of prefixyyyy.
// If success, return true and output the full path of the directory created.
diff --git a/base/file_util_posix.cc b/base/file_util_posix.cc
index edb20c7..e440640 100644
--- a/base/file_util_posix.cc
+++ b/base/file_util_posix.cc
@@ -267,6 +267,13 @@ bool CreateTemporaryFileName(std::wstring* temp_file) {
return true;
}
+bool CreateTemporaryFileNameInDir(const std::wstring& dir,
+ std::wstring* temp_file) {
+ // Not implemented yet.
+ NOTREACHED();
+ return false;
+}
+
bool CreateNewTempDirectory(const std::wstring& prefix,
std::wstring* new_temp_path) {
std::wstring tmpdir;
diff --git a/base/file_util_win.cc b/base/file_util_win.cc
index 8253e53..7be172f 100644
--- a/base/file_util_win.cc
+++ b/base/file_util_win.cc
@@ -383,13 +383,19 @@ bool GetTempDir(std::wstring* path) {
}
bool CreateTemporaryFileName(std::wstring* temp_file) {
- wchar_t temp_name[MAX_PATH + 1];
std::wstring temp_path;
if (!GetTempDir(&temp_path))
return false;
- if (!GetTempFileName(temp_path.c_str(), L"", 0, temp_name))
+ return CreateTemporaryFileNameInDir(temp_path, temp_file);
+}
+
+bool CreateTemporaryFileNameInDir(const std::wstring& dir,
+ std::wstring* temp_file) {
+ wchar_t temp_name[MAX_PATH + 1];
+
+ if (!GetTempFileName(dir.c_str(), L"", 0, temp_name))
return false; // fail!
DWORD path_len = GetLongPathName(temp_name, temp_name, MAX_PATH);
diff --git a/base/string_util.cc b/base/string_util.cc
index 223c485..5fa2f75 100644
--- a/base/string_util.cc
+++ b/base/string_util.cc
@@ -1434,3 +1434,39 @@ size_t base::wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) {
return lcpyT<wchar_t>(dst, src, dst_size);
}
+bool ElideString(const std::wstring& input, int max_len, std::wstring* output) {
+ DCHECK(max_len >= 0);
+ if (static_cast<int>(input.length()) <= max_len) {
+ output->assign(input);
+ return false;
+ }
+
+ switch (max_len) {
+ case 0:
+ output->clear();
+ break;
+ case 1:
+ output->assign(input.substr(0, 1));
+ break;
+ case 2:
+ output->assign(input.substr(0, 2));
+ break;
+ case 3:
+ output->assign(input.substr(0, 1) + L"." +
+ input.substr(input.length() - 1));
+ break;
+ case 4:
+ output->assign(input.substr(0, 1) + L".." +
+ input.substr(input.length() - 1));
+ break;
+ default: {
+ int rstr_len = (max_len - 3) / 2;
+ int lstr_len = rstr_len + ((max_len - 3) % 2);
+ output->assign(input.substr(0, lstr_len) + L"..." +
+ input.substr(input.length() - rstr_len));
+ break;
+ }
+ }
+
+ return true;
+}
diff --git a/base/string_util.h b/base/string_util.h
index a9f08c4..981cd30 100644
--- a/base/string_util.h
+++ b/base/string_util.h
@@ -508,6 +508,14 @@ std::wstring ReplaceStringPlaceholders(const std::wstring& format_string,
const std::wstring& d,
std::vector<size_t>* offsets);
+// If the size of |input| is more than |max_len|, this function returns true and
+// |input| is shortened into |output| by removing chars in the middle (they are
+// replaced with up to 3 dots, as size permits).
+// Ex: ElideString(L"Hello", 10, &str) puts Hello in str and returns false.
+// ElideString(L"Hello my name is Tom", 10, &str) puts "Hell...Tom" in str and
+// returns true.
+bool ElideString(const std::wstring& input, int max_len, std::wstring* output);
+
// Returns true if the string passed in matches the pattern. The pattern
// string can contain wildcards like * and ?
// TODO(iyengar) This function may not work correctly for CJK strings as
diff --git a/base/string_util_unittest.cc b/base/string_util_unittest.cc
index e438ebb..25f43c6 100644
--- a/base/string_util_unittest.cc
+++ b/base/string_util_unittest.cc
@@ -1380,3 +1380,29 @@ TEST(StringUtilTest, WprintfFormatPortabilityTest) {
}
}
+TEST(StringUtilTest, ElideString) {
+ struct TestData {
+ const wchar_t* input;
+ int max_len;
+ bool result;
+ const wchar_t* output;
+ } cases[] = {
+ { L"Hello", 0, true, L"" },
+ { L"", 0, false, L"" },
+ { L"Hello, my name is Tom", 1, true, L"H" },
+ { L"Hello, my name is Tom", 2, true, L"He" },
+ { L"Hello, my name is Tom", 3, true, L"H.m" },
+ { L"Hello, my name is Tom", 4, true, L"H..m" },
+ { L"Hello, my name is Tom", 5, true, L"H...m" },
+ { L"Hello, my name is Tom", 6, true, L"He...m" },
+ { L"Hello, my name is Tom", 7, true, L"He...om" },
+ { L"Hello, my name is Tom", 10, true, L"Hell...Tom" },
+ { L"Hello, my name is Tom", 100, false, L"Hello, my name is Tom" }
+ };
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
+ std::wstring output;
+ EXPECT_EQ(cases[i].result,
+ ElideString(cases[i].input, cases[i].max_len, &output));
+ EXPECT_TRUE(output == cases[i].output);
+ }
+}