ksString.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. {*******************************************************}
  2. { }
  3. { Methods of Sting }
  4. { }
  5. { CopyRight (C) 2018-2020 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): 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): string;
  128. var
  129. I: Integer;
  130. sPath, sExt: string;
  131. begin
  132. Result := AFile;
  133. I := 0;
  134. sPath := ChangeFileExt(Result, '');
  135. sExt := ExtractFileExt(Result);
  136. while FileExists(Result) or DirectoryExists(Result) do begin
  137. Inc(I);
  138. Result := Format('%s(%d)%s', [sPath, I, sExt]);
  139. end;
  140. end;
  141. procedure ChangeInvalidFileNameChars(var FileName: string; AChar: Char);
  142. var
  143. PFileName: PChar;
  144. FileNameLen: Integer;
  145. Ch: Char;
  146. I: Integer;
  147. begin
  148. I := 0;
  149. PFileName := PChar(FileName);
  150. FileNameLen := Length(FileName);
  151. while I < FileNameLen do begin
  152. Ch := PFileName[I];
  153. if not TPath.IsValidFileNameChar(Ch) then
  154. PFileName[I] := AChar
  155. else
  156. Inc(I);
  157. end;
  158. end;
  159. function CleanFileName(const Filename: string): string;
  160. var
  161. s: string;
  162. i: integer;
  163. begin
  164. Result := ExtractFileName(Filename);
  165. Result := ChangeFileExt(Result, '');
  166. // 处理 () 情况
  167. s := Copy(Result, Length(Result) - 2, 3);
  168. if (Copy(s, 1, 1) = '(') and (Copy(s, Length(s), 1) = ')') then begin
  169. i := StrToIntDef(Copy(s, 2, 1), 0);
  170. if (10 > i) and (i > 0) then
  171. Result := Copy(Result, 1, Length(Result) - 3);
  172. end;
  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. Result := Trim(Result);
  181. end;
  182. function FormatSeconds(const AValue: Double): string; overload;
  183. var
  184. h, m, s, ms: Integer;
  185. begin
  186. ms := Trunc(Frac(AValue) * 1000);
  187. s := Trunc(AValue);
  188. m := s div 60;
  189. h := m div 60;
  190. if h > 0 then
  191. Result := Format('%.2d:%.2d:%.2d.%.3d', [h, m mod 60, s mod 60, ms])
  192. else
  193. Result := Format('%.2d:%.2d.%.3d', [m, s mod 60, ms]);
  194. end;
  195. function FormatSeconds(const AValue: Int64): string;
  196. var
  197. h, m, s: Integer;
  198. begin
  199. s := AValue;
  200. m := s div 60;
  201. h := m div 60;
  202. if h > 0 then
  203. Result := Format('%.2d:%.2d:%.2d', [h, m mod 60, s mod 60])
  204. else
  205. Result := Format('%.2d:%.2d', [m, s mod 60]);
  206. end;
  207. function FormatSeconds(const AValue: Integer): string; overload;
  208. begin
  209. FormatSeconds(Int64(AValue));
  210. end;
  211. function FormatSize(ASize: Int64): string;
  212. var
  213. AIdx, R1, s1: Int64;
  214. AIsNeg: Boolean;
  215. const
  216. Units: array [0 .. 6] of string = ('EB', 'PB', 'TB', 'GB', 'MB', 'KB', 'B');
  217. begin
  218. AIsNeg := (ASize < 0);
  219. AIdx := 6;
  220. R1 := 0;
  221. if AIsNeg then
  222. ASize := -ASize;
  223. Result := '';
  224. while (AIdx >= 0) do
  225. begin
  226. s1 := ASize mod 1024;
  227. ASize := ASize shr 10;
  228. if (ASize = 0) or (AIdx = 0) then
  229. begin
  230. R1 := R1 * 100 div 1024;
  231. if R1 > 0 then
  232. begin
  233. if R1 >= 10 then
  234. Result := IntToStr(s1) + '.' + IntToStr(R1) + Units[AIdx]
  235. else
  236. Result := IntToStr(s1) + '.' + '0' + IntToStr(R1) + Units[AIdx];
  237. end
  238. else
  239. Result := IntToStr(s1) + Units[AIdx];
  240. Break;
  241. end;
  242. R1 := s1;
  243. Dec(AIdx);
  244. end;
  245. if AIsNeg then
  246. Result := '-' + Result;
  247. end;
  248. function IsChineseMobileNumber(S: string; AOnlyNum: Boolean): boolean;
  249. var
  250. p: PChar;
  251. i: Integer;
  252. begin
  253. Result := False;
  254. // 最短肯定是11位
  255. if (Length(S) < 11) then
  256. Exit;
  257. i := 0;
  258. p := PChar(S);
  259. while p^ <> #0 do begin
  260. if p^ = '+' then begin
  261. if i > 0 then // +号必须在第一位
  262. Exit;
  263. if (p[1] = '8') and (p[2] = '6') then // +86,而且是连续的
  264. Inc(p, 3)
  265. else
  266. Exit;
  267. end
  268. else if ((p^ >= '0') and (p^ <= '9')) then begin
  269. if (i = 0) and (p^ <> '1') then // 中国手机号都是1开头
  270. Exit;
  271. Inc(p);
  272. Inc(i);
  273. end
  274. else if (not AOnlyNum) and ((p^ = '-') or (p^ = ' ')) then
  275. Inc(p)
  276. else
  277. Exit;
  278. end;
  279. Result := i = 11; //中国手机号 11位
  280. end;
  281. function SizeUnitToInt(AUnit: Char): Int64;
  282. const
  283. Units: array [0 .. 6] of Char = ('b', 'k', 'm', 'g', 't', 'p', 'e');
  284. var
  285. I: Integer;
  286. LUnit: Char;
  287. begin
  288. Result := 1;
  289. case AUnit of
  290. 'A'..'Z':
  291. LUnit := Char(Word(AUnit) or $0020);
  292. else
  293. LUnit := AUnit;
  294. end;
  295. for I := Low(Units) to High(Units) do begin
  296. if (I > 0) then
  297. Result := Result * 1024;
  298. if LUnit = Units[I] then
  299. Exit;
  300. end;
  301. Result := 1;
  302. end;
  303. function SizeUnitToInt(AUnit: string): Int64;
  304. var
  305. LSize: string;
  306. begin
  307. Result := 1;
  308. LSize := Trim(AUnit);
  309. case Length(LSize) of
  310. 1,2: Result := SizeUnitToInt(PChar(AUnit)^);
  311. end;
  312. end;
  313. function SizeStrToInt(ASize: string; ADefault: Int64): Int64;
  314. var
  315. p: PChar;
  316. c: Char;
  317. s: string;
  318. begin
  319. Result := ADefault;
  320. if Length(ASize) = 0 then
  321. Exit;
  322. s := '';
  323. c := #0;
  324. p := PChar(ASize);
  325. while p^ <> #0 do
  326. case p^ of
  327. 'b', 'k', 'm', 'g', 't', 'p', 'e',
  328. 'B', 'K', 'M', 'G', 'T', 'P', 'E':
  329. begin
  330. if s = '' then
  331. Exit;
  332. if c <> #0 then
  333. Exit;
  334. c := p^;
  335. Inc(p);
  336. if (p^ = 'b') or (p^ = 'B') then
  337. Inc(p);
  338. end;
  339. '0' .. '9':
  340. begin
  341. if c <> #0 then
  342. Exit;
  343. s := s + p^;
  344. Inc(p);
  345. end;
  346. ',':
  347. begin
  348. if c <> #0 then
  349. Exit;
  350. Inc(p);
  351. end;
  352. ' ':
  353. Inc(p);
  354. else
  355. Exit;
  356. end;
  357. if s <> '' then
  358. Result := StrToInt64Def(s, Result) * SizeUnitToInt(c);
  359. end;
  360. function GetStrParamStr(P: PChar; var Param: String): PChar;
  361. function Extract(P: PChar; const Buffer: PChar; var Len: Integer): PChar;
  362. var
  363. InQuote: Boolean;
  364. begin
  365. Len := 0;
  366. InQuote := False;
  367. while (P^ <> #0) and ((P^ > ' ') or InQuote) do begin
  368. if P^ = '"' then
  369. InQuote := not InQuote
  370. else begin
  371. if Assigned(Buffer) then
  372. Buffer[Len] := P^;
  373. Inc(Len);
  374. end;
  375. Inc(P);
  376. end;
  377. Result := P;
  378. end;
  379. var
  380. Len: Integer;
  381. Buffer: String;
  382. begin
  383. Extract(P, nil, Len);
  384. SetString(Buffer, nil, Len);
  385. Result := Extract(P, @Buffer[1], Len);
  386. Param := Buffer;
  387. while (Result^ <> #0) and (Result^ <= ' ') do
  388. Inc(Result);
  389. end;
  390. function GetStrParamVal(Command: PChar; const Param, Default: String): String;
  391. var
  392. I, PCount: Integer;
  393. Z: String;
  394. begin
  395. PCount := StrParamCount(Command);
  396. for I := 0 to PCount - 1 do
  397. begin
  398. Z := StrParamStr(Command, I);
  399. if (StrLIComp(PChar(Z), PChar('/' + Param + '='), Length(Param) + 2) = 0)
  400. or (StrLIComp(PChar(Z), PChar('-' + Param + '='), Length(Param) + 2) = 0) then
  401. begin
  402. Delete(Z, 1, Length(Param) + 2);
  403. Result := Z;
  404. Exit;
  405. end;
  406. end;
  407. Result := Default;
  408. end;
  409. function GetStrParamVal(Command: string; const Param, Default: String): String;
  410. begin
  411. Result := GetStrParamVal(PChar(Command), Param, Default);
  412. end;
  413. function StrParamCount(Command: PChar): Integer;
  414. var
  415. P: PChar;
  416. S: String;
  417. begin
  418. Result := 0;
  419. P := Command;
  420. while (P^ <> #0) and (P^ <= ' ') do
  421. Inc(P);
  422. while P^ <> #0 do begin
  423. P := GetStrParamStr(P, S);
  424. Inc(Result);
  425. end;
  426. end;
  427. function StrParamCount(Command: string): Integer;
  428. begin
  429. Result := StrParamCount(PChar(Command));
  430. end;
  431. function StrParamStr(Command: PChar; Index: Integer): string;
  432. { Returns the Indexth command line parameter, or an empty string if Index is
  433. out of range.
  434. Differences from Delphi's ParamStr:
  435. - No limits on parameter length
  436. - Doesn't ignore empty parameters ("")
  437. - Handles the empty argv[0] case like MSVC: if GetCommandLine() returns
  438. " a b" then NewParamStr(1) should return "a", not "b" }
  439. var
  440. Buffer: array[0..MAX_PATH-1] of Char;
  441. S: String;
  442. P: PChar;
  443. begin
  444. if Index <= 0 then begin
  445. Result := '';
  446. end
  447. else begin
  448. P := Command;
  449. while True do begin
  450. if P^ = #0 then begin
  451. S := '';
  452. Break;
  453. end;
  454. P := GetStrParamStr(P, S);
  455. if Index = 0 then Break;
  456. Dec(Index);
  457. end;
  458. Result := S;
  459. end;
  460. end;
  461. function StrParamStr(Command: string; Index: Integer): string;
  462. begin
  463. Result := StrParamStr(PChar(Command), Index);
  464. end;
  465. end.