Delphi-Projects
303 строки · 6.8 Кб
1unit Russian;
2
3interface
4
5function RNeedDosToWin(const S: string): Boolean;
6function RWin(const S: string): string;
7function RDos(const S: string): string;
8procedure RDosToWin(var S: string);
9procedure RWinToDos(var S: string);
10
11function RUpper(const S: string): string;
12function RLower(const S: string): string;
13procedure RUpper1st(var S: string);
14
15function RStrToCurr(const S: string): Currency;
16function RStrToCurrStr(const S: string): string;
17function RStrToInt(const S: string): Integer;
18function RStrToDate(const S: string): TDateTime;
19function RStrToDateStr(const S: string): string;
20
21function RCurrToStr(const V: Currency; const Delim: string = '.'): string;
22function RDateToStr(const D: TDateTime): string; overload;
23function RDateToStr(const S: string): string; overload;
24function RDateToS(const D: TDateTime): string; overload;
25function RDateToS(const S: string): string; overload;
26
27function REndStr(N: Integer; const S1, S2, S: string): string;
28
29implementation
30
31uses
32SysUtils;
33
34function RNeedDosToWin(const S: string): Boolean;
35var
36B: Byte;
37I, IWin, IDos: Integer;
38begin
39IWin := 0;
40IDos := 0;
41for I := 1 to Length(S) do
42begin
43B := Byte(S[I]);
44if B > 127 then
45if B < 176 then //DOS А128..Яа..п175
46Inc(IDos)
47else if B > 191 then //WIN А192..Я223
48if (B < 224) or (B > 239) then //WIN р240..я255
49Inc(IWin);
50//common DOS р224..я239 & WIN а224..п239 ignored
51end;
52Result := IDos > IWin;
53end;
54
55function RWin(const S: string): string;
56begin
57Result := S;
58RDosToWin(Result);
59end;
60
61function RDos(const S: string): string;
62begin
63Result := S;
64RWinToDos(Result);
65end;
66
67procedure RDosToWin(var S: string);
68var
69B: Byte;
70I: Integer;
71begin
72for I := 1 to Length(S) do
73begin
74B := Byte(S[I]);
75if B > 127 then
76if B < 176 then //А128..Яа..п175
77Inc(S[I], 64)
78else if B > 223 then //р224..я239
79begin
80if B < 240 then
81Inc(S[I], 16)
82else if B = 240 then //Ё240
83S[I] := #168 //'Е'
84else if B = 241 then //ё241
85S[I] := #184 //'е'
86end
87else if B in [193, 194, 196] then
88S[I] := #45 //'-'
89else if B in [179, 180, 195, 197] then
90S[I] := #124 //'|'
91else if B in [191, 192, 217, 218] then
92S[I] := #43; //'+'
93end;
94end;
95
96procedure RWinToDos(var S: string);
97var
98B: Byte;
99I: Integer;
100begin
101for I := 1 to Length(S) do
102begin
103B := Byte(S[I]);
104if B > 127 then
105if B > 239 then //р240..я255
106Dec(S[I], 16)
107else if B > 191 then //А191..Яа..п239
108Dec(S[I], 64)
109else if B = 168 then //Ё240
110S[I] := #133 //Е133
111else if B = 184 then //ё241
112S[I] := #165 //ё165
113else if B = 185 then //No
114S[I] := #78 //N
115else if B in [150, 151] then
116S[I] := #45; //--
117end;
118end;
119
120function RUpper(const S: string): string;
121var
122I: Integer;
123begin
124Result := S;
125for I := 1 to Length(S) do
126if S[I] >= #224 then
127Dec(Result[I], 32);
128end;
129
130function RLower(const S: string): string;
131var
132I: Integer;
133begin
134Result := S;
135for I := 1 to Length(S) do
136if (S[I] >= #192) and (S[I] <= #223) then
137Inc(Result[I], 32);
138end;
139
140procedure RUpper1st(var S: string);
141begin
142if S[1] >= #224 then
143Dec(S[1], 32);
144end;
145
146{Читает всю строку с цифрами и возвращает денежное значение,
147независимо от наличия пробелов и букв в этой строке,
148но дробная часть отделяется после *последней* точки,
149запятой, знака равенства или минуса...}
150function RStrToCurr(const S: string): Currency;
151const
152ValidChars = '1234567890.,-=';
153var
154I, C, N, P: Integer;
155begin
156Result := 0;
157N := 0;
158P := 0;
159for I := 1 to Length(S) do
160begin
161C := Pos(S[I], ValidChars);
162if C > 10 then
163P := N
164else if C = 10 then begin
165Result := Result * 10;
166Inc(N);
167end
168else if C > 0 then begin
169Result := Result * 10 + C;
170Inc(N);
171end;
172end;
173if P > 0 then
174while N > P do
175begin
176Result := Result / 10;
177Dec(N);
178end;
179if S[1] = '-' then Result := -Result;
180end;
181
182function RStrToInt(const S: string): Integer;
183const
184ValidChars = '1234567890';
185var
186I, C: Integer;
187begin
188Result := 0;
189if S = '' then
190Exit;
191for I := 1 to Length(S) do
192begin
193C := Pos(S[I], ValidChars);
194if C = 10 then
195Result := Result * 10
196else if C > 0 then
197Result := Result * 10 + C;
198end;
199if S[1] = '-' then Result := -Result;
200end;
201
202{Читает дату почти в любом возможном числовом написании}
203function RStrToDate(const S: string): TDateTime;
204const
205ValidChars = '1234567890./-';
206var
207I, P, C: Integer;
208SS: array[0..2] of string;
209DD, MM, YY: Word;
210begin
211P := 0;
212SS[0] := '';
213SS[1] := '';
214SS[2] := '';
215for I := 1 to Length(S) do
216begin
217C := Pos(S[I], ValidChars);
218if C > 10 then
219Inc(P)
220else if C > 0 then
221SS[P] := SS[P] + S[I];
222end;
223
224DecodeDate(Date, YY, MM, DD); //this year by default
225if Length(SS[0]) = 8 then //YYYYMMDD
226begin
227I := StrToInt(SS[0]);
228DD := I mod 100;
229MM := I div 100 mod 100;
230YY := I div 10000;
231end
232else if Length(SS[0]) = 4 then begin //YYYY-MM-DD
233DD := StrToInt(SS[2]);
234MM := StrToInt(SS[1]);
235YY := StrToInt(SS[0]);
236end
237else if Length(SS[2]) > 0 then begin //DD.MM.[[YY]YY]
238DD := StrToInt(SS[0]);
239MM := StrToInt(SS[1]);
240if Length(SS[2]) = 4 then
241YY := StrToInt(SS[2])
242else if Length(SS[2]) = 2 then
243begin
244I := StrToInt(SS[2]);
245YY := YY div 100 * 100 + I;
246if I < TwoDigitYearCenturyWindow then
247Dec(YY, 100);
248end;
249end;
250Result := EncodeDate(YY, MM, DD);
251end;
252
253function RStrToDateStr(const S: string): string;
254begin
255Result := DateToStr(RStrToDate(S));
256end;
257
258function RStrToCurrStr(const S: string): string;
259begin
260Result := Format('%.2n', [RStrToCurr(S)]);
261end;
262
263function RCurrToStr(const V: Currency; const Delim: string = '.'): string;
264begin
265Result := Format('%.2f', [V]);
266Result[Length(Result)-2] := Delim[1];
267end;
268
269function RDateToStr(const D: TDateTime): string;
270begin
271Result := FormatDateTime('dd.mm.yyyy', D);
272end;
273
274function RDateToStr(const S: string): string;
275begin
276Result := FormatDateTime('dd.mm.yyyy', RStrToDate(S));
277end;
278
279function RDateToS(const D: TDateTime): string;
280begin
281Result := FormatDateTime('yyyymmdd', D);
282end;
283
284function RDateToS(const S: string): string;
285begin
286Result := FormatDateTime('yyyymmdd', RStrToDate(S));
287end;
288
289function REndStr(N: Integer; const S1, S2, S: string): string;
290begin
291if N > 100 then
292N := N mod 100;
293if N > 19 then
294N := N mod 10;
295case N of
2961: Result := S1;
2972..4: Result := S2;
298else
299Result := S;
300end;
301end;
302
303end.
304