Advertisement
NosefU

Untitled

Nov 29th, 2014
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 9.81 KB | None | 0 0
  1. unit main_unit;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
  8.  
  9. const
  10.      hex = ['0'..'9','A'..'F','a'..'f'];
  11.  
  12. type
  13.   TForm1 = class(TForm)
  14.     Edit1: TEdit;
  15.     Edit2: TEdit;
  16.     Button1: TButton;
  17.     Label1: TLabel;
  18.     Label2: TLabel;
  19.     Label3: TLabel;
  20.     procedure Button1Click(Sender: TObject);
  21.     procedure Edit1KeyPress(Sender: TObject; var Key: Char);
  22.     procedure Edit1Change(Sender: TObject);
  23.   private
  24.     { Private declarations }
  25.   public
  26.     { Public declarations }
  27.   end;
  28.  
  29. var
  30.   Form1: TForm1;
  31.  
  32. implementation
  33.  
  34. {$R *.dfm}
  35.  
  36.  
  37. //перевод символа в код в соответствии с таблицей знакогенератора
  38. //можно было бы поизвращаться, и придумать что-то элегантное,
  39. //но решение задачи "в лоб" было проще в обслуживании
  40. function charCode(const input: char): string;
  41. var
  42.   output: string;
  43. begin
  44.      output:='';
  45.      if input='А' then output:='A'
  46.                                      else if input='Б' then output:='\xa0'
  47.      else if input='В' then output:='B'
  48.                                      else if input='Г' then output:='\xa1'
  49.                                      else if input='Д' then output:='\xe0'
  50.      else if input='Е' then output:='E'
  51.                                      else if input='Ё' then output:='\xa2'
  52.                                      else if input='Ж' then output:='\xa3'
  53.                                      else if input='З' then output:='\xa4'
  54.                                      else if input='И' then output:='\xa5'
  55.                                      else if input='Й' then output:='\xa6'
  56.      else if input='К' then output:='K'
  57.                                      else if input='Л' then output:='\xa7'
  58.      else if input='М' then output:='M'
  59.      else if input='Н' then output:='H'
  60.      else if input='О' then output:='O'
  61.                                      else if input='П' then output:='\xa8'
  62.      else if input='Р' then output:='P'
  63.      else if input='С' then output:='C'
  64.      else if input='Т' then output:='T'
  65.                                      else if input='У' then output:='\xa9'
  66.                                      else if input='Ф' then output:='\xaa'
  67.      else if input='Х' then output:='X'
  68.                                      else if input='Ц' then output:='\xe1'
  69.                                      else if input='Ч' then output:='\xab'
  70.                                      else if input='Ш' then output:='\xac'
  71.                                      else if input='Щ' then output:='\xe2'
  72.                                      else if input='Ъ' then output:='\xad'
  73.                                      else if input='Ы' then output:='\xae'
  74.                                      else if input='Ь' then output:='\x08'
  75.                                      else if input='Э' then output:='\xaf'
  76.                                      else if input='Ю' then output:='\xb0'
  77.                                      else if input='Я' then output:='\xb1'
  78.  
  79.      else if input='а' then output:='a'
  80.                                      else if input='б' then output:='\xb2'
  81.                                      else if input='в' then output:='\xb3'
  82.                                      else if input='г' then output:='\xb4'
  83.                                      else if input='д' then output:='\xe3'
  84.      else if input='е' then output:='e'
  85.                                      else if input='ё' then output:='\xb5'
  86.                                      else if input='ж' then output:='\xb6'
  87.                                      else if input='з' then output:='\xb7'
  88.                                      else if input='и' then output:='\xb8'
  89.                                      else if input='й' then output:='\xb9'
  90.                                      else if input='к' then output:='\xba'
  91.                                      else if input='л' then output:='\xbb'
  92.                                      else if input='м' then output:='\xbc'
  93.                                      else if input='н' then output:='\xbd'
  94.      else if input='о' then output:='o'
  95.                                      else if input='п' then output:='\xbe'
  96.      else if input='р' then output:='p'
  97.      else if input='с' then output:='c'
  98.                                      else if input='т' then output:='\xbf'
  99.      else if input='у' then output:='y'
  100.                                      else if input='ф' then output:='\xe4'
  101.      else if input='х' then output:='x'
  102.                                      else if input='ц' then output:='\xe5'
  103.                                      else if input='ч' then output:='\xc0'
  104.                                      else if input='ш' then output:='\xc1'
  105.                                      else if input='щ' then output:='\xe6'
  106.                                      else if input='ъ' then output:='\xc2'
  107.                                      else if input='ы' then output:='\xc3'
  108.                                      else if input='ь' then output:='\xc4'
  109.                                      else if input='э' then output:='\xc5'
  110.                                      else if input='ю' then output:='\xc6'
  111.                                      else if input='я' then output:='\xc7'
  112.  
  113.      //шесть строк - и мы уже поддерживаем украинский и белорусский.
  114.      //только с ґ у разработчиков дисплея не сложилось
  115.                                      else if input='Ў' then output:='\x0a'
  116.                                      else if input='ў' then output:='\x0a'
  117.                                      else if input='Ї' then output:='\x0c'
  118.                                      else if input='ї' then output:='\x0d'
  119.                                      else if input='Є' then output:='\xoe'
  120.                                      else if input='є' then output:='\x0f'
  121.      else output:=input;
  122.  
  123.      //если символ конвертируется в код,
  124.      //либо в обратный слеш - ставим после них двойные кавычки.
  125.      //кавычки после слеша нужны для того, чтобы мы могли
  126.      //выводить конструкции, похожие на код символа, на дисплей: /""xaf => /xaf
  127.      if ((output<>input) and not(output[1] in hex)) or (output = '\') then
  128.         output := output + '""';
  129.  
  130.  
  131.      result:=output;
  132.  
  133. end;
  134.  
  135. procedure TForm1.Button1Click(Sender: TObject);
  136. var
  137.   inp_str, outp_str: string;
  138.   i: integer;
  139.  
  140. begin
  141.      inp_str:=Edit1.Text;
  142.      outp_str:='';
  143.  
  144.      for i:=1 to length(inp_str) do
  145.               outp_str:= outp_str + charCode(inp_str[i]) ;
  146.  
  147.  
  148. //форматирование строки
  149.  
  150.       Insert('"', outp_str, 1);
  151.  
  152.      //чистим двойные кавычки в конце, предварительно поставив одни,
  153.      //если вдруг таковых не окажется вообще
  154.      if outp_str[length(outp_str)]<>'"' then
  155.         outp_str:= outp_str + '"'
  156.         else if (outp_str[length(outp_str)]='"') and (outp_str[length(outp_str)-1]='"')
  157.           then Delete(outp_str, length(outp_str), 1);
  158.  
  159.  
  160.      //чистим текст от четырех кавычек подряд (на стыке кириллических символов),
  161.      //оставляя двое
  162.      i:=1;
  163.      while i>0 do
  164.      begin
  165.           i := Pos('""""', outp_str);
  166.           Delete(outp_str, i, 2);
  167.      end;
  168.  
  169.      //оптимизируем строку в соответствии с требованиями дисплея
  170.      i:= 5;
  171.      while i<length(outp_str) do
  172.      begin
  173.  
  174.           //избавляемся от кавычек между символом, который может быть hex-значением,
  175.           //и символом, стоящим перед ним, который hex быть не может
  176.           //знаю, что можно было всё впихнуть в одно условие, но мне так удобнее
  177.           if outp_str[i] in hex then
  178.               if (outp_str[i-1] = '"') and (outp_str[i-2] = '"') then
  179.                 if not(outp_str[i-3] in hex) then
  180.                   Delete(outp_str, i-2, 2);
  181.  
  182.           //избавляемся от кавычек перед символами, которые не могут быть hex-значениями
  183.           if not(outp_str[i] in hex) and (outp_str[i]<>'x') then
  184.             if (outp_str[i-1] = '"') and (outp_str[i-2] = '"') then
  185.               Delete(outp_str, i-2, 2);
  186.  
  187.            //избавляемся от кавычек перед кодами символов
  188.            if (outp_str[i] = '\') and ( outp_str[i+1] = 'x') then
  189.              if (outp_str[i-1] = '"') and (outp_str[i-2] = '"') then
  190.               Delete(outp_str, i-2, 2);
  191.             inc(i);
  192.  
  193.      end;
  194.  
  195.      Edit2.Text := outp_str;
  196.  
  197.      //выделим результат и перекинем фокус в поле с результатом
  198.        Edit2.SelStart:=1;
  199.        Edit2.SelLength:= length(Edit2.Text);
  200.        Edit2.SetFocus;
  201. end;
  202.  
  203. //считаем количество введенных символов в поле с исходным текстом
  204. procedure TForm1.Edit1Change(Sender: TObject);
  205. begin
  206.   Label1.Caption := inttostr(length(Edit1.Text));
  207. end;
  208.  
  209. //обрабатываем нажатие Enter в поле с исходным текстом
  210. procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
  211. begin
  212. if Key = #13 then
  213.     Button1Click(Self);
  214. end;
  215.  
  216. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement