ksString.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. {*******************************************************}
  2. { }
  3. { Methods of Sting }
  4. { }
  5. { CopyRight (C) 2018-2023 KngStr }
  6. { }
  7. { Some Code from }
  8. { QDAC of swish, Inno }
  9. { Thanks }
  10. { }
  11. {*******************************************************}
  12. unit ksString;
  13. interface
  14. /// <summary>
  15. /// 创建空白文件
  16. /// </summary>
  17. function CreateBlankFile(S: string): Boolean;
  18. /// <summary>
  19. /// 自动为已存在文件名加数字
  20. /// </summary>
  21. function AutoChangeFileName(AFile: string; AHaveExt: Boolean = True; AFormat: string = '(%d)'): string;
  22. /// <summary>
  23. /// 自动替换文件名中的非法字符为
  24. /// </summary>
  25. procedure ChangeInvalidFileNameChars(var FileName: string; AChar: Char = '_');
  26. /// <summary>
  27. /// 清理文件名中的 (1) [1]
  28. /// (1)系统命名,[1]浏览器下载命名,甚至[1] (2)
  29. /// </summary>
  30. function CleanFileName(const Filename: string): string;
  31. /// <summary>
  32. /// 格式化秒为时间,冒号分割
  33. /// </summary>
  34. function FormatSeconds(const AValue: Int64): string; overload;
  35. /// <summary>
  36. /// 格式化秒为时间,冒号分割
  37. /// </summary>
  38. function FormatSeconds(const AValue: Integer): string; overload;
  39. /// <summary>
  40. /// 格式化秒为时间,冒号分割
  41. /// </summary>
  42. function FormatSeconds(const AValue: Double): string; overload;
  43. /// <summary>
  44. /// 格式化文件大小,拷贝自qdac,增加更多单位
  45. /// </summary>
  46. function FormatSize(ASize: Int64): string;
  47. /// <summary>
  48. /// 文件大小字符串转数字
  49. /// 1[b] "1 k[b]" " 1 m[b] " 1g[b] 1t[b]
  50. /// </summary>
  51. function SizeStrToInt(ASize: string; ADefault: Int64 = 0): Int64;
  52. /// <summary>
  53. /// 文件大小单位转数字
  54. /// </summary>
  55. function SizeUnitToInt(AUnit: string): Int64; overload;
  56. /// <summary>
  57. /// 文件大小单位转数字
  58. /// </summary>
  59. function SizeUnitToInt(AUnit: Char): Int64; overload;
  60. /// <summary>
  61. /// 是否中国手机号
  62. /// 13900000000
  63. /// +8613900000000
  64. /// +86 139 0000 0000
  65. /// +86-139-0000-0000
  66. /// </summary>
  67. /// <param name="AOnlyNum">仅允许数字和开头的+</param>
  68. function IsChineseMobileNumber(S: string; AOnlyNum: Boolean = False): Boolean;
  69. {
  70. 文本参数解析,来自Inno
  71. Differences from Delphi's ParamStr:
  72. - No limits on parameter length
  73. - Doesn't ignore empty parameters ("")
  74. - Handles the empty argv[0] case like MSVC: if GetCommandLine() returns
  75. " a b" then NewParamStr(1) should return "a", not "b"
  76. }
  77. /// <summary>
  78. /// 获取指定参数, 0-base
  79. /// </summary>
  80. /// <remarks>
  81. /// 按命令行拆分方式
  82. /// </remarks>
  83. function StrParamStr(Command: string; Index: Integer): string; overload;
  84. function StrParamStr(Command: PChar; Index: Integer): string; overload;
  85. /// <summary>
  86. /// 获取参数个数, 包含第一个参数
  87. /// </summary>
  88. /// <remarks>
  89. /// 按命令行拆分方式
  90. /// </remarks>
  91. function StrParamCount(Command: string): Integer; overload;
  92. function StrParamCount(Command: PChar): Integer; overload;
  93. /// <summary>
  94. /// 获取指定命令行参数的值,/x=xxx 和 -x=xxx
  95. /// </summary>
  96. /// <remarks>
  97. /// 按命令行拆分方式
  98. /// </remarks>
  99. function GetStrParamVal(Command: string; const Param, Default: String): String; overload;
  100. function GetStrParamVal(Command: PChar; const Param, Default: String): String; overload;
  101. /// <summary>
  102. /// 获取下一个参数,并返回剩余内容
  103. /// </summary>
  104. /// <remarks>
  105. /// 按命令行拆分方式
  106. /// </remarks>
  107. function GetStrParamStr(P: PChar; var Param: string): PChar;
  108. implementation
  109. uses
  110. {$IFDEF MSWINDOWS}Windows,{$ENDIF}
  111. Classes, SysUtils, IOUtils;
  112. function CreateBlankFile(S: string): Boolean;
  113. var
  114. FStrm: TFileStream;
  115. begin
  116. Result := False;
  117. try
  118. FStrm := TFileStream.Create(S, fmCreate);
  119. if FStrm = nil then
  120. Exit;
  121. Result := True;
  122. FreeAndNil(FStrm);
  123. except
  124. FStrm := nil;
  125. end;
  126. end;
  127. function AutoChangeFileName(AFile: string; AHaveExt: Boolean; AFormat: string): string;
  128. var
  129. I: Integer;
  130. sPath, sExt: string;
  131. begin
  132. Result := AFile;
  133. if AHaveExt then begin
  134. sPath := ChangeFileExt(Result, '');
  135. sExt := ExtractFileExt(Result);
  136. end
  137. else begin
  138. sPath := Result;
  139. sExt := '';
  140. end;
  141. // 注意:目录名不能写死到format里
  142. // 因为什么文件名都有,避免引起格式化错误
  143. I := 0;
  144. while FileExists(Result) or DirectoryExists(Result) do begin
  145. Inc(I);
  146. Result := Format('%s%s%s', [sPath, Format(AFormat, [I]), sExt]);
  147. end;
  148. end;
  149. procedure ChangeInvalidFileNameChars(var FileName: string; AChar: Char);
  150. var
  151. PFileName: PChar;
  152. FileNameLen: Integer;
  153. Ch: Char;
  154. I: Integer;
  155. begin
  156. I := 0;
  157. PFileName := PChar(FileName);
  158. FileNameLen := Length(FileName);
  159. while I < FileNameLen do begin
  160. Ch := PFileName[I];
  161. if not TPath.IsValidFileNameChar(Ch) then
  162. PFileName[I] := AChar;
  163. Inc(I);
  164. end;
  165. end;
  166. function CleanFileName(const Filename: string): string;
  167. var
  168. s: string;
  169. i: integer;
  170. begin
  171. Result := ExtractFileName(Filename);
  172. Result := ChangeFileExt(Result, '');
  173. // 处理 () 情况
  174. s := Copy(Result, Length(Result) - 2, 3);
  175. if (Copy(s, 1, 1) = '(') and (Copy(s, Length(s), 1) = ')') then begin
  176. i := StrToIntDef(Copy(s, 2, 1), 0);
  177. if (10 > i) and (i > 0) then
  178. Result := Copy(Result, 1, Length(Result) - 3);
  179. end;
  180. // 处理 [] 情况
  181. s := Copy(Result, Length(Result) - 2, 3);
  182. if (Copy(s, 1, 1) = '[') and (Copy(s, Length(s), 1) = ']') then begin
  183. i := StrToIntDef(Copy(s, 2, 1), 0);
  184. if (10 > i) and (i > 0) then
  185. Result := Copy(Result, 1, Length(Result) - 3);
  186. end;
  187. Result := Trim(Result);
  188. end;
  189. function FormatSeconds(const AValue: Double): string; overload;
  190. var
  191. h, m, s, ms: Integer;
  192. begin
  193. ms := Trunc(Frac(AValue) * 1000);
  194. s := Trunc(AValue);
  195. m := s div 60;
  196. h := m div 60;
  197. if h > 0 then
  198. Result := Format('%.2d:%.2d:%.2d.%.3d', [h, m mod 60, s mod 60, ms])
  199. else
  200. Result := Format('%.2d:%.2d.%.3d', [m, s mod 60, ms]);
  201. end;
  202. function FormatSeconds(const AValue: Int64): string;
  203. var
  204. h, m, s: Integer;
  205. begin
  206. s := AValue;
  207. m := s div 60;
  208. h := m div 60;
  209. if h > 0 then
  210. Result := Format('%.2d:%.2d:%.2d', [h, m mod 60, s mod 60])
  211. else
  212. Result := Format('%.2d:%.2d', [m, s mod 60]);
  213. end;
  214. function FormatSeconds(const AValue: Integer): string; overload;
  215. begin
  216. FormatSeconds(Int64(AValue));
  217. end;
  218. function FormatSize(ASize: Int64): string;
  219. var
  220. AIdx, R1, s1: Int64;
  221. AIsNeg: Boolean;
  222. const
  223. Units: array [0 .. 6] of string = ('EB', 'PB', 'TB', 'GB', 'MB', 'KB', 'B');
  224. begin
  225. AIsNeg := (ASize < 0);
  226. AIdx := 6;
  227. R1 := 0;
  228. if AIsNeg then
  229. ASize := -ASize;
  230. Result := '';
  231. while (AIdx >= 0) do
  232. begin
  233. s1 := ASize mod 1024;
  234. ASize := ASize shr 10;
  235. if (ASize = 0) or (AIdx = 0) then
  236. begin
  237. R1 := R1 * 100 div 1024;
  238. if R1 > 0 then
  239. begin
  240. if R1 >= 10 then
  241. Result := IntToStr(s1) + '.' + IntToStr(R1) + Units[AIdx]
  242. else
  243. Result := IntToStr(s1) + '.' + '0' + IntToStr(R1) + Units[AIdx];
  244. end
  245. else
  246. Result := IntToStr(s1) + Units[AIdx];
  247. Break;
  248. end;
  249. R1 := s1;
  250. Dec(AIdx);
  251. end;
  252. if AIsNeg then
  253. Result := '-' + Result;
  254. end;
  255. function IsChineseMobileNumber(S: string; AOnlyNum: Boolean): boolean;
  256. var
  257. p: PChar;
  258. i: Integer;
  259. begin
  260. Result := False;
  261. // 最短肯定是11位
  262. if (Length(S) < 11) then
  263. Exit;
  264. i := 0;
  265. p := PChar(S);
  266. while p^ <> #0 do begin
  267. if p^ = '+' then begin
  268. if i > 0 then // +号必须在第一位
  269. Exit;
  270. if (p[1] = '8') and (p[2] = '6') then // +86,而且是连续的
  271. Inc(p, 3)
  272. else
  273. Exit;
  274. end
  275. else if ((p^ >= '0') and (p^ <= '9')) then begin
  276. if (i = 0) and (p^ <> '1') then // 中国手机号都是1开头
  277. Exit;
  278. Inc(p);
  279. Inc(i);
  280. end
  281. else if (not AOnlyNum) and ((p^ = '-') or (p^ = ' ')) then
  282. Inc(p)
  283. else
  284. Exit;
  285. end;
  286. Result := i = 11; //中国手机号 11位
  287. end;
  288. function SizeUnitToInt(AUnit: Char): Int64;
  289. const
  290. Units: array [0 .. 6] of Char = ('b', 'k', 'm', 'g', 't', 'p', 'e');
  291. var
  292. I: Integer;
  293. LUnit: Char;
  294. begin
  295. Result := 1;
  296. case AUnit of
  297. 'A'..'Z':
  298. LUnit := Char(Word(AUnit) or $0020);
  299. else
  300. LUnit := AUnit;
  301. end;
  302. for I := Low(Units) to High(Units) do begin
  303. if (I > 0) then
  304. Result := Result * 1024;
  305. if LUnit = Units[I] then
  306. Exit;
  307. end;
  308. Result := 1;
  309. end;
  310. function SizeUnitToInt(AUnit: string): Int64;
  311. var
  312. LSize: string;
  313. begin
  314. Result := 1;
  315. LSize := Trim(AUnit);
  316. case Length(LSize) of
  317. 1,2: Result := SizeUnitToInt(PChar(AUnit)^);
  318. end;
  319. end;
  320. function SizeStrToInt(ASize: string; ADefault: Int64): Int64;
  321. var
  322. p: PChar;
  323. c: Char;
  324. s: string;
  325. begin
  326. Result := ADefault;
  327. if Length(ASize) = 0 then
  328. Exit;
  329. s := '';
  330. c := #0;
  331. p := PChar(ASize);
  332. while p^ <> #0 do
  333. case p^ of
  334. 'b', 'k', 'm', 'g', 't', 'p', 'e',
  335. 'B', 'K', 'M', 'G', 'T', 'P', 'E':
  336. begin
  337. if s = '' then
  338. Exit;
  339. if c <> #0 then
  340. Exit;
  341. c := p^;
  342. Inc(p);
  343. if (p^ = 'b') or (p^ = 'B') then
  344. Inc(p);
  345. end;
  346. '0' .. '9':
  347. begin
  348. if c <> #0 then
  349. Exit;
  350. s := s + p^;
  351. Inc(p);
  352. end;
  353. ',':
  354. begin
  355. if c <> #0 then
  356. Exit;
  357. Inc(p);
  358. end;
  359. ' ':
  360. Inc(p);
  361. else
  362. Exit;
  363. end;
  364. if s <> '' then
  365. Result := StrToInt64Def(s, Result) * SizeUnitToInt(c);
  366. end;
  367. function GetStrParamStr(P: PChar; var Param: String): PChar;
  368. function Extract(P: PChar; const Buffer: PChar; var Len: Integer): PChar;
  369. var
  370. InQuote: Boolean;
  371. begin
  372. Len := 0;
  373. InQuote := False;
  374. while (P^ <> #0) and ((P^ > ' ') or InQuote) do begin
  375. if P^ = '"' then
  376. InQuote := not InQuote
  377. else begin
  378. if Assigned(Buffer) then
  379. Buffer[Len] := P^;
  380. Inc(Len);
  381. end;
  382. Inc(P);
  383. end;
  384. Result := P;
  385. end;
  386. var
  387. Len: Integer;
  388. Buffer: String;
  389. begin
  390. Extract(P, nil, Len);
  391. SetString(Buffer, nil, Len);
  392. Result := Extract(P, @Buffer[1], Len);
  393. Param := Buffer;
  394. while (Result^ <> #0) and (Result^ <= ' ') do
  395. Inc(Result);
  396. end;
  397. function GetStrParamVal(Command: PChar; const Param, Default: String): String;
  398. var
  399. I, PCount: Integer;
  400. Z: String;
  401. begin
  402. PCount := StrParamCount(Command);
  403. for I := 0 to PCount - 1 do
  404. begin
  405. Z := StrParamStr(Command, I);
  406. if (StrLIComp(PChar(Z), PChar('/' + Param + '='), Length(Param) + 2) = 0)
  407. or (StrLIComp(PChar(Z), PChar('-' + Param + '='), Length(Param) + 2) = 0) then
  408. begin
  409. Delete(Z, 1, Length(Param) + 2);
  410. Result := Z;
  411. Exit;
  412. end;
  413. end;
  414. Result := Default;
  415. end;
  416. function GetStrParamVal(Command: string; const Param, Default: String): String;
  417. begin
  418. Result := GetStrParamVal(PChar(Command), Param, Default);
  419. end;
  420. function StrParamCount(Command: PChar): Integer;
  421. var
  422. P: PChar;
  423. S: String;
  424. begin
  425. Result := 0;
  426. P := Command;
  427. while (P^ <> #0) and (P^ <= ' ') do
  428. Inc(P);
  429. while P^ <> #0 do begin
  430. P := GetStrParamStr(P, S);
  431. Inc(Result);
  432. end;
  433. end;
  434. function StrParamCount(Command: string): Integer;
  435. begin
  436. Result := StrParamCount(PChar(Command));
  437. end;
  438. function StrParamStr(Command: PChar; Index: Integer): string;
  439. { Returns the Indexth command line parameter, or an empty string if Index is
  440. out of range.
  441. Differences from Delphi's ParamStr:
  442. - No limits on parameter length
  443. - Doesn't ignore empty parameters ("")
  444. - Handles the empty argv[0] case like MSVC: if GetCommandLine() returns
  445. " a b" then NewParamStr(1) should return "a", not "b" }
  446. var
  447. Buffer: array[0..MAX_PATH-1] of Char;
  448. S: String;
  449. P: PChar;
  450. begin
  451. if Index <= 0 then begin
  452. Result := '';
  453. end
  454. else begin
  455. P := Command;
  456. while True do begin
  457. if P^ = #0 then begin
  458. S := '';
  459. Break;
  460. end;
  461. P := GetStrParamStr(P, S);
  462. if Index = 0 then Break;
  463. Dec(Index);
  464. end;
  465. Result := S;
  466. end;
  467. end;
  468. function StrParamStr(Command: string; Index: Integer): string;
  469. begin
  470. Result := StrParamStr(PChar(Command), Index);
  471. end;
  472. end.