ksString.pas 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. {*******************************************************}
  2. { }
  3. { Methods of Sting }
  4. { }
  5. { CopyRight (C) 2018-2020 KngStr }
  6. { }
  7. { Some Code from }
  8. { QDAC of swish }
  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. /// 格式化秒为时间,冒号分割
  28. /// </summary>
  29. function FormatSeconds(const AValue: Int64): string; overload;
  30. /// <summary>
  31. /// 格式化秒为时间,冒号分割
  32. /// </summary>
  33. function FormatSeconds(const AValue: Integer): string; overload;
  34. /// <summary>
  35. /// 格式化秒为时间,冒号分割
  36. /// </summary>
  37. function FormatSeconds(const AValue: Double): string; overload;
  38. /// <summary>
  39. /// 格式化文件大小,拷贝自qdac,增加更多单位
  40. /// </summary>
  41. function FormatSize(ASize: Int64): string;
  42. /// <summary>
  43. /// 文件大小字符串转数字
  44. /// 1[b] "1 k[b]" " 1 m[b] " 1g[b] 1t[b]
  45. /// </summary>
  46. function SizeStrToInt(ASize: string; ADefault: Int64 = 0): Int64;
  47. /// <summary>
  48. /// 文件大小单位转数字
  49. /// </summary>
  50. function SizeUnitToInt(AUnit: string): Int64; overload;
  51. /// <summary>
  52. /// 文件大小单位转数字
  53. /// </summary>
  54. function SizeUnitToInt(AUnit: Char): Int64; overload;
  55. /// <summary>
  56. /// 是否中国手机号
  57. /// 13900000000
  58. /// +8613900000000
  59. /// +86 139 0000 0000
  60. /// +86-139-0000-0000
  61. /// </summary>
  62. /// <param name="AOnlyNum">仅允许数字和开头的+</param>
  63. function IsChineseMobileNumber(S: string; AOnlyNum: Boolean = False): Boolean;
  64. implementation
  65. uses
  66. Classes, SysUtils, IOUtils;
  67. function CreateBlankFile(S: string): Boolean;
  68. var
  69. FStrm: TFileStream;
  70. begin
  71. Result := False;
  72. try
  73. FStrm := TFileStream.Create(S, fmCreate);
  74. if FStrm = nil then
  75. Exit;
  76. Result := True;
  77. FreeAndNil(FStrm);
  78. except
  79. FStrm := nil;
  80. end;
  81. end;
  82. function AutoChangeFileName(AFile: string): string;
  83. var
  84. I: Integer;
  85. sPath, sExt: string;
  86. begin
  87. Result := AFile;
  88. I := 0;
  89. sPath := ChangeFileExt(Result, '');
  90. sExt := ExtractFileExt(Result);
  91. while FileExists(Result) do begin
  92. Inc(I);
  93. Result := Format('%s(%d)%s', [sPath, I, sExt]);
  94. end;
  95. end;
  96. procedure ChangeInvalidFileNameChars(var FileName: string; AChar: Char);
  97. var
  98. PFileName: PChar;
  99. FileNameLen: Integer;
  100. Ch: Char;
  101. I: Integer;
  102. begin
  103. I := 0;
  104. PFileName := PChar(FileName);
  105. FileNameLen := Length(FileName);
  106. while I < FileNameLen do begin
  107. Ch := PFileName[I];
  108. if not TPath.IsValidFileNameChar(Ch) then
  109. PFileName[I] := AChar
  110. else
  111. Inc(I);
  112. end;
  113. end;
  114. function FormatSeconds(const AValue: Double): string; overload;
  115. var
  116. h, m, s, ms: Integer;
  117. begin
  118. ms := Trunc(Frac(AValue) * 1000);
  119. s := Trunc(AValue);
  120. m := s div 60;
  121. h := m div 60;
  122. if h > 0 then
  123. Result := Format('%.2d:%.2d:%.2d.%.3d', [h, m mod 60, s mod 60, ms])
  124. else
  125. Result := Format('%.2d:%.2d.%.3d', [m, s mod 60, ms]);
  126. end;
  127. function FormatSeconds(const AValue: Int64): string;
  128. var
  129. h, m, s: Integer;
  130. begin
  131. s := AValue;
  132. m := s div 60;
  133. h := m div 60;
  134. if h > 0 then
  135. Result := Format('%.2d:%.2d:%.2d', [h, m mod 60, s mod 60])
  136. else
  137. Result := Format('%.2d:%.2d', [m, s mod 60]);
  138. end;
  139. function FormatSeconds(const AValue: Integer): string; overload;
  140. begin
  141. FormatSeconds(Int64(AValue));
  142. end;
  143. function FormatSize(ASize: Int64): string;
  144. var
  145. AIdx, R1, s1: Int64;
  146. AIsNeg: Boolean;
  147. const
  148. Units: array [0 .. 6] of string = ('EB', 'PB', 'TB', 'GB', 'MB', 'KB', 'B');
  149. begin
  150. AIsNeg := (ASize < 0);
  151. AIdx := 6;
  152. R1 := 0;
  153. if AIsNeg then
  154. ASize := -ASize;
  155. Result := '';
  156. while (AIdx >= 0) do
  157. begin
  158. s1 := ASize mod 1024;
  159. ASize := ASize shr 10;
  160. if (ASize = 0) or (AIdx = 0) then
  161. begin
  162. R1 := R1 * 100 div 1024;
  163. if R1 > 0 then
  164. begin
  165. if R1 >= 10 then
  166. Result := IntToStr(s1) + '.' + IntToStr(R1) + Units[AIdx]
  167. else
  168. Result := IntToStr(s1) + '.' + '0' + IntToStr(R1) + Units[AIdx];
  169. end
  170. else
  171. Result := IntToStr(s1) + Units[AIdx];
  172. Break;
  173. end;
  174. R1 := s1;
  175. Dec(AIdx);
  176. end;
  177. if AIsNeg then
  178. Result := '-' + Result;
  179. end;
  180. function IsChineseMobileNumber(S: string; AOnlyNum: Boolean): boolean;
  181. var
  182. p: PChar;
  183. i: Integer;
  184. begin
  185. Result := False;
  186. // 最短肯定是11位
  187. if (Length(S) < 11) then
  188. Exit;
  189. i := 0;
  190. p := PChar(S);
  191. while p^ <> #0 do begin
  192. if p^ = '+' then begin
  193. if i > 0 then // +号必须在第一位
  194. Exit;
  195. if (p[1] = '8') and (p[2] = '6') then // +86,而且是连续的
  196. Inc(p, 3)
  197. else
  198. Exit;
  199. end
  200. else if ((p^ >= '0') and (p^ <= '9')) then begin
  201. if (i = 0) and (p^ <> '1') then // 中国手机号都是1开头
  202. Exit;
  203. Inc(p);
  204. Inc(i);
  205. end
  206. else if (not AOnlyNum) and ((p^ = '-') or (p^ = ' ')) then
  207. Inc(p)
  208. else
  209. Exit;
  210. end;
  211. Result := i = 11; //中国手机号 11位
  212. end;
  213. function SizeUnitToInt(AUnit: Char): Int64;
  214. const
  215. Units: array [0 .. 6] of Char = ('b', 'k', 'm', 'g', 't', 'p', 'e');
  216. var
  217. I: Integer;
  218. LUnit: Char;
  219. begin
  220. Result := 1;
  221. case AUnit of
  222. 'A'..'Z':
  223. LUnit := Char(Word(AUnit) or $0020);
  224. else
  225. LUnit := AUnit;
  226. end;
  227. for I := Low(Units) to High(Units) do begin
  228. if (I > 0) then
  229. Result := Result * 1024;
  230. if LUnit = Units[I] then
  231. Exit;
  232. end;
  233. Result := 1;
  234. end;
  235. function SizeUnitToInt(AUnit: string): Int64;
  236. var
  237. LSize: string;
  238. begin
  239. Result := 1;
  240. LSize := Trim(AUnit);
  241. case Length(LSize) of
  242. 1,2: Result := SizeUnitToInt(LSize[Low(LSize)]);
  243. end;
  244. end;
  245. function SizeStrToInt(ASize: string; ADefault: Int64): Int64;
  246. var
  247. p: PChar;
  248. c: Char;
  249. s: string;
  250. begin
  251. Result := ADefault;
  252. if Length(ASize) = 0 then
  253. Exit;
  254. s := '';
  255. c := #0;
  256. p := PChar(ASize);
  257. while p^ <> #0 do
  258. case p^ of
  259. 'b', 'k', 'm', 'g', 't', 'p', 'e',
  260. 'B', 'K', 'M', 'G', 'T', 'P', 'E':
  261. begin
  262. if s = '' then
  263. Exit;
  264. if c <> #0 then
  265. Exit;
  266. c := p^;
  267. Inc(p);
  268. if (p^ = 'b') or (p^ = 'B') then
  269. Inc(p);
  270. end;
  271. '0' .. '9':
  272. begin
  273. if c <> #0 then
  274. Exit;
  275. s := s + p^;
  276. Inc(p);
  277. end;
  278. ',':
  279. begin
  280. if c <> #0 then
  281. Exit;
  282. Inc(p);
  283. end;
  284. ' ':
  285. Inc(p);
  286. else
  287. Exit;
  288. end;
  289. if s <> '' then
  290. Result := StrToUInt64Def(s, Result) * SizeUnitToInt(c);
  291. end;
  292. end.