Subversion Repositories Kolibri OS

Rev

Rev 9902 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. (*
  2.     Copyright 2021 Anton Krotov
  3.  
  4.     This file is part of CEdit.
  5.  
  6.     CEdit is free software: you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation, either version 3 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     CEdit is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with CEdit. If not, see <http://www.gnu.org/licenses/>.
  18. *)
  19.  
  20. MODULE Search;
  21.  
  22. IMPORT
  23.     CB := Clipboard, List, Utils, SYSTEM;
  24.  
  25.  
  26. TYPE
  27.  
  28.     tBuffer* = CB.tBuffer;
  29.  
  30.     IdxTable* = ARRAY 65536, 2 OF INTEGER;
  31.  
  32.     tPos* = POINTER TO RECORD (List.tItem)
  33.         pos*: INTEGER
  34.     END;
  35.  
  36.  
  37. PROCEDURE index* (text: tBuffer; VAR table: IdxTable; cs: BOOLEAN): tBuffer;
  38. VAR
  39.     pChar, cnt, i: INTEGER;
  40.     c: WCHAR;
  41.     res: tBuffer;
  42. BEGIN
  43.     pChar := text.dataPtr;
  44.     cnt := CB.bufSize(text) DIV 2;
  45.  
  46.     FOR i := 0 TO 65535 DO
  47.         table[i, 1] := 0
  48.     END;
  49.  
  50.     i := cnt;
  51.     WHILE i > 0 DO
  52.         SYSTEM.GET(pChar, c);
  53.         IF ~cs & Utils.cap(c) THEN
  54.             SYSTEM.PUT(pChar, c)
  55.         END;
  56.         INC(table[ORD(c), 1]);
  57.         INC(pChar, 2);
  58.         DEC(i)
  59.     END;
  60.  
  61.     res := CB.create(cnt * SYSTEM.SIZE(INTEGER));
  62.  
  63.     table[0, 0] := res.dataPtr;
  64.     FOR i := 1 TO 65535 DO
  65.         table[i, 0] := table[i - 1, 0] + table[i - 1, 1] * SYSTEM.SIZE(INTEGER)
  66.     END;
  67.  
  68.     pChar := text.dataPtr;
  69.     i := 0;
  70.     WHILE i < cnt DO
  71.         SYSTEM.GET(pChar, c);
  72.         SYSTEM.PUT(table[ORD(c), 0], i);
  73.         INC(table[ORD(c), 0], SYSTEM.SIZE(INTEGER));
  74.         INC(pChar, 2);
  75.         INC(i)
  76.     END;
  77.  
  78.     FOR i := 0 TO 65535 DO
  79.         DEC(table[i, 0], table[i, 1] * SYSTEM.SIZE(INTEGER))
  80.     END
  81.  
  82.     RETURN res
  83. END index;
  84.  
  85.  
  86. PROCEDURE find* (text: tBuffer; table: IdxTable; s: ARRAY OF WCHAR; whole: BOOLEAN; list: List.tList);
  87. VAR
  88.     k, pos, n, x, prev_item_pos: INTEGER;
  89.     item: tPos;
  90.     c1, c2: WCHAR;
  91.     flag: BOOLEAN;
  92. BEGIN
  93.     n := LENGTH(s);
  94.     k := table[ORD(s[0]), 1];
  95.     pos := table[ORD(s[0]), 0];
  96.     prev_item_pos := 0;
  97.     WHILE k > 0 DO
  98.         SYSTEM.GET(pos, x);
  99.         IF Utils.streq(text.dataPtr + x*2, SYSTEM.ADR(s[0]), n) THEN
  100.             flag := whole;
  101.             IF flag THEN
  102.                 IF x > 0 THEN
  103.                     SYSTEM.GET(text.dataPtr + (x - 1)*2, c1);
  104.                 ELSE
  105.                     c1 := 0X
  106.                 END;
  107.                 SYSTEM.GET(text.dataPtr + (x + n)*2, c2);
  108.                 flag := Utils.isLetter(c1) OR Utils.isLetter(c2) OR Utils.isDigit(c1) OR Utils.isDigit(c2) OR (c1 = "_") OR (c2 = "_")
  109.             END;
  110.             IF ~flag & (x >= prev_item_pos) THEN
  111.                 prev_item_pos := x + n;
  112.                 NEW(item);
  113.                 item.pos := x;
  114.                 List.append(list, item)
  115.             END
  116.         END;
  117.         INC(pos, SYSTEM.SIZE(INTEGER));
  118.         DEC(k)
  119.     END
  120. END find;
  121.  
  122.  
  123. END Search.