幸运的是,Chromium 是开源的,因此您可以查看记录良好的代码。好的...我找到了:这里
RenameAndUniquify
是:
void DownloadFileImpl::RenameAndUniquify(
const base::FilePath& full_path,
const RenameCompletionCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
base::FilePath new_path(full_path);
int uniquifier =
file_util::GetUniquePathNumber(new_path, FILE_PATH_LITERAL(""));
if (uniquifier > 0) {
new_path = new_path.InsertBeforeExtensionASCII(
base::StringPrintf(" (%d)", uniquifier));
}
...
}
并InsertBeforeExtension
致电ExtensionSeperatorPosition
您感兴趣的电话(链接):
// Find the position of the '.' that separates the extension from the rest
// of the file name. The position is relative to BaseName(), not value().
// This allows a second extension component of up to 4 characters when the
// rightmost extension component is a common double extension (gz, bz2, Z).
// For example, foo.tar.gz or foo.tar.Z would have extension components of
// '.tar.gz' and '.tar.Z' respectively. Returns npos if it can't find an
// extension.
StringType::size_type ExtensionSeparatorPosition(const StringType& path) {
// Special case "." and ".."
if (path == FilePath::kCurrentDirectory || path == FilePath::kParentDirectory)
return StringType::npos;
const StringType::size_type last_dot =
path.rfind(FilePath::kExtensionSeparator);
// No extension, or the extension is the whole filename.
if (last_dot == StringType::npos || last_dot == 0U)
return last_dot;
const StringType::size_type penultimate_dot =
path.rfind(FilePath::kExtensionSeparator, last_dot - 1);
const StringType::size_type last_separator =
path.find_last_of(FilePath::kSeparators, last_dot - 1,
arraysize(FilePath::kSeparators) - 1);
if (penultimate_dot == StringType::npos ||
(last_separator != StringType::npos &&
penultimate_dot < last_separator)) {
return last_dot;
}
for (size_t i = 0; i < arraysize(kCommonDoubleExtensions); ++i) {
StringType extension(path, penultimate_dot + 1);
if (LowerCaseEqualsASCII(extension, kCommonDoubleExtensions[i]))
return penultimate_dot;
}
StringType extension(path, last_dot + 1);
for (size_t i = 0; i < arraysize(kCommonDoubleExtensionSuffixes); ++i) {
if (LowerCaseEqualsASCII(extension, kCommonDoubleExtensionSuffixes[i])) {
if ((last_dot - penultimate_dot) <= 5U &&
(last_dot - penultimate_dot) > 1U) {
return penultimate_dot;
}
}
}
return last_dot;
}