File tree Expand file tree Collapse file tree 4 files changed +74
-1
lines changed Expand file tree Collapse file tree 4 files changed +74
-1
lines changed Original file line number Diff line number Diff line change @@ -440,6 +440,51 @@ std::string Dirname(const std::string &full_path)
440
440
return full_path.substr (start_pos, end_pos - start_pos);
441
441
}
442
442
443
+ /* *
444
+ * 目标:
445
+ * "a" -> "a"
446
+ * "a.txt" -> "a"
447
+ * "/some/a.txt" -> "a"
448
+ * "/some/a.b/c" -> "c"
449
+ * "" -> ""
450
+ */
451
+ std::string GetFileStem (const std::string &filepath)
452
+ {
453
+ std::string::size_type start_pos = 0 ;
454
+
455
+ auto last_backslash_pos = filepath.find_last_of (' /' );
456
+ if (last_backslash_pos != std::string::npos)
457
+ start_pos = last_backslash_pos + 1 ;
458
+
459
+ auto last_dot_pos = filepath.find_last_of (' .' );
460
+ if (last_dot_pos != std::string::npos && last_dot_pos > start_pos)
461
+ return filepath.substr (start_pos, last_dot_pos - start_pos);
462
+ else
463
+ return filepath.substr (start_pos);
464
+ }
465
+
466
+ /* *
467
+ * 目标:
468
+ * "a.b" -> "b"
469
+ * "a" -> ""
470
+ * "a." -> ""
471
+ * "/w/a.b" -> "b"
472
+ * "" -> ""
473
+ */
474
+ std::string GetFileSuffix (const std::string &filepath)
475
+ {
476
+ auto last_dot_pos = filepath.find_last_of (' .' );
477
+ if (last_dot_pos == std::string::npos)
478
+ return " " ;
479
+
480
+ // ! 防止 "a.b/c" -> "a/c" 的情况
481
+ auto last_backslash_pos = filepath.find_last_of (' /' );
482
+ if (last_backslash_pos != std::string::npos && last_backslash_pos > last_dot_pos)
483
+ return " " ;
484
+
485
+ return filepath.substr (last_dot_pos + 1 );
486
+ }
487
+
443
488
bool Rename (const std::string &old_name, const std::string &new_name)
444
489
{
445
490
int ret = ::rename (old_name.c_str (), new_name.c_str ());
Original file line number Diff line number Diff line change @@ -275,6 +275,16 @@ const char* Basename(const char *full_path);
275
275
*/
276
276
std::string Dirname (const std::string &full_path);
277
277
278
+ /* *
279
+ * 获取文件名的主体,如:"config.json" -> "config"
280
+ */
281
+ std::string GetFileStem (const std::string &file_path);
282
+
283
+ /* *
284
+ * 获取文件名的后缀,如:"config.json" -> "json"
285
+ */
286
+ std::string GetFileSuffix (const std::string &file_path);
287
+
278
288
/* *
279
289
* 重命名
280
290
*
Original file line number Diff line number Diff line change @@ -228,6 +228,24 @@ TEST(fs, Basename) {
228
228
EXPECT_EQ (Basename (std::string ()), " " );
229
229
}
230
230
231
+ TEST (fs, GetFileStem) {
232
+ EXPECT_EQ (GetFileStem (" a.b" ), " a" );
233
+ EXPECT_EQ (GetFileStem (" a" ), " a" );
234
+ EXPECT_EQ (GetFileStem (" a." ), " a" );
235
+ EXPECT_EQ (GetFileStem (" /w/a.b" ), " a" );
236
+ EXPECT_EQ (GetFileStem (" /w.x/a" ), " a" );
237
+ EXPECT_EQ (GetFileStem (" " ), " " );
238
+ }
239
+
240
+ TEST (fs, GetFileSuffix) {
241
+ EXPECT_EQ (GetFileSuffix (" a.b" ), " b" );
242
+ EXPECT_EQ (GetFileSuffix (" a" ), " " );
243
+ EXPECT_EQ (GetFileSuffix (" a." ), " " );
244
+ EXPECT_EQ (GetFileSuffix (" /w/a.b" ), " b" );
245
+ EXPECT_EQ (GetFileSuffix (" /w.x/a" ), " " );
246
+ EXPECT_EQ (GetFileSuffix (" " ), " " );
247
+ }
248
+
231
249
TEST (fs, RemoveDirectory) {
232
250
// ! 绝对路径测试
233
251
int ret = 0 ;
Original file line number Diff line number Diff line change 21
21
# TBOX版本号
22
22
TBOX_VERSION_MAJOR := 1
23
23
TBOX_VERSION_MINOR := 12
24
- TBOX_VERSION_REVISION := 9
24
+ TBOX_VERSION_REVISION := 10
You can’t perform that action at this time.
0 commit comments