Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit main_unit;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
- const
- hex = ['0'..'9','A'..'F','a'..'f'];
- type
- TForm1 = class(TForm)
- Edit1: TEdit;
- Edit2: TEdit;
- Button1: TButton;
- Label1: TLabel;
- Label2: TLabel;
- Label3: TLabel;
- procedure Button1Click(Sender: TObject);
- procedure Edit1KeyPress(Sender: TObject; var Key: Char);
- procedure Edit1Change(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- //перевод символа в код в соответствии с таблицей знакогенератора
- //можно было бы поизвращаться, и придумать что-то элегантное,
- //но решение задачи "в лоб" было проще в обслуживании
- function charCode(const input: char): string;
- var
- output: string;
- begin
- output:='';
- if input='А' then output:='A'
- else if input='Б' then output:='\xa0'
- else if input='В' then output:='B'
- else if input='Г' then output:='\xa1'
- else if input='Д' then output:='\xe0'
- else if input='Е' then output:='E'
- else if input='Ё' then output:='\xa2'
- else if input='Ж' then output:='\xa3'
- else if input='З' then output:='\xa4'
- else if input='И' then output:='\xa5'
- else if input='Й' then output:='\xa6'
- else if input='К' then output:='K'
- else if input='Л' then output:='\xa7'
- else if input='М' then output:='M'
- else if input='Н' then output:='H'
- else if input='О' then output:='O'
- else if input='П' then output:='\xa8'
- else if input='Р' then output:='P'
- else if input='С' then output:='C'
- else if input='Т' then output:='T'
- else if input='У' then output:='\xa9'
- else if input='Ф' then output:='\xaa'
- else if input='Х' then output:='X'
- else if input='Ц' then output:='\xe1'
- else if input='Ч' then output:='\xab'
- else if input='Ш' then output:='\xac'
- else if input='Щ' then output:='\xe2'
- else if input='Ъ' then output:='\xad'
- else if input='Ы' then output:='\xae'
- else if input='Ь' then output:='\x08'
- else if input='Э' then output:='\xaf'
- else if input='Ю' then output:='\xb0'
- else if input='Я' then output:='\xb1'
- else if input='а' then output:='a'
- else if input='б' then output:='\xb2'
- else if input='в' then output:='\xb3'
- else if input='г' then output:='\xb4'
- else if input='д' then output:='\xe3'
- else if input='е' then output:='e'
- else if input='ё' then output:='\xb5'
- else if input='ж' then output:='\xb6'
- else if input='з' then output:='\xb7'
- else if input='и' then output:='\xb8'
- else if input='й' then output:='\xb9'
- else if input='к' then output:='\xba'
- else if input='л' then output:='\xbb'
- else if input='м' then output:='\xbc'
- else if input='н' then output:='\xbd'
- else if input='о' then output:='o'
- else if input='п' then output:='\xbe'
- else if input='р' then output:='p'
- else if input='с' then output:='c'
- else if input='т' then output:='\xbf'
- else if input='у' then output:='y'
- else if input='ф' then output:='\xe4'
- else if input='х' then output:='x'
- else if input='ц' then output:='\xe5'
- else if input='ч' then output:='\xc0'
- else if input='ш' then output:='\xc1'
- else if input='щ' then output:='\xe6'
- else if input='ъ' then output:='\xc2'
- else if input='ы' then output:='\xc3'
- else if input='ь' then output:='\xc4'
- else if input='э' then output:='\xc5'
- else if input='ю' then output:='\xc6'
- else if input='я' then output:='\xc7'
- //шесть строк - и мы уже поддерживаем украинский и белорусский.
- //только с ґ у разработчиков дисплея не сложилось
- else if input='Ў' then output:='\x0a'
- else if input='ў' then output:='\x0a'
- else if input='Ї' then output:='\x0c'
- else if input='ї' then output:='\x0d'
- else if input='Є' then output:='\xoe'
- else if input='є' then output:='\x0f'
- else output:=input;
- //если символ конвертируется в код,
- //либо в обратный слеш - ставим после них двойные кавычки.
- //кавычки после слеша нужны для того, чтобы мы могли
- //выводить конструкции, похожие на код символа, на дисплей: /""xaf => /xaf
- if ((output<>input) and not(output[1] in hex)) or (output = '\') then
- output := output + '""';
- result:=output;
- end;
- procedure TForm1.Button1Click(Sender: TObject);
- var
- inp_str, outp_str: string;
- i: integer;
- begin
- inp_str:=Edit1.Text;
- outp_str:='';
- for i:=1 to length(inp_str) do
- outp_str:= outp_str + charCode(inp_str[i]) ;
- //форматирование строки
- Insert('"', outp_str, 1);
- //чистим двойные кавычки в конце, предварительно поставив одни,
- //если вдруг таковых не окажется вообще
- if outp_str[length(outp_str)]<>'"' then
- outp_str:= outp_str + '"'
- else if (outp_str[length(outp_str)]='"') and (outp_str[length(outp_str)-1]='"')
- then Delete(outp_str, length(outp_str), 1);
- //чистим текст от четырех кавычек подряд (на стыке кириллических символов),
- //оставляя двое
- i:=1;
- while i>0 do
- begin
- i := Pos('""""', outp_str);
- Delete(outp_str, i, 2);
- end;
- //оптимизируем строку в соответствии с требованиями дисплея
- i:= 5;
- while i<length(outp_str) do
- begin
- //избавляемся от кавычек между символом, который может быть hex-значением,
- //и символом, стоящим перед ним, который hex быть не может
- //знаю, что можно было всё впихнуть в одно условие, но мне так удобнее
- if outp_str[i] in hex then
- if (outp_str[i-1] = '"') and (outp_str[i-2] = '"') then
- if not(outp_str[i-3] in hex) then
- Delete(outp_str, i-2, 2);
- //избавляемся от кавычек перед символами, которые не могут быть hex-значениями
- if not(outp_str[i] in hex) and (outp_str[i]<>'x') then
- if (outp_str[i-1] = '"') and (outp_str[i-2] = '"') then
- Delete(outp_str, i-2, 2);
- //избавляемся от кавычек перед кодами символов
- if (outp_str[i] = '\') and ( outp_str[i+1] = 'x') then
- if (outp_str[i-1] = '"') and (outp_str[i-2] = '"') then
- Delete(outp_str, i-2, 2);
- inc(i);
- end;
- Edit2.Text := outp_str;
- //выделим результат и перекинем фокус в поле с результатом
- Edit2.SelStart:=1;
- Edit2.SelLength:= length(Edit2.Text);
- Edit2.SetFocus;
- end;
- //считаем количество введенных символов в поле с исходным текстом
- procedure TForm1.Edit1Change(Sender: TObject);
- begin
- Label1.Caption := inttostr(length(Edit1.Text));
- end;
- //обрабатываем нажатие Enter в поле с исходным текстом
- procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
- begin
- if Key = #13 then
- Button1Click(Self);
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement