1 package gnu.jemacs.swing; 2 import gnu.jemacs.buffer.*; 3 import javax.swing.text.*; 4 import javax.swing.undo.*; 5 import gnu.lists.*; 6 7 12 13 public class BufferContent extends gnu.kawa.swingviews.SwingContent 14 { 15 public BufferContent() 16 { 17 this(100); 18 } 19 20 public BufferContent(int initialSize) 21 { 22 super(initialSize); 23 } 24 25 public static int indexOf(char[] buffer, int start, int limit, char ch) 26 { 27 for (int i = start; i < limit; i++) 28 { 29 if (buffer[i] == ch) 30 return i; 31 } 32 return -1; 33 } 34 35 37 public static int lastIndexOf(char[] buffer, int start, int limit, char ch) 38 { 39 for (int i = start; i >= limit; i--) 40 { 41 if (buffer[i] == ch) 42 return i; 43 } 44 return -1; 45 } 46 47 61 62 public final long scan(char target, int start, int end, 63 int count, boolean allowQuit) 64 { 65 CharBuffer b = buffer; 66 int limit = end > b.gapStart ? end + b.gapEnd - b.gapStart : end; 67 if (start > b.gapStart) 68 start += b.gapEnd - b.gapStart; 69 if (count > 0) 70 { 71 while (start < limit && count > 0) 72 { 73 int ceil; 74 if (start == b.gapStart) 75 start = b.gapEnd; 76 if (start < b.gapStart && limit > b.gapStart) 77 ceil = b.gapStart; 78 else 79 { 80 ceil = limit; 81 } 82 if (allowQuit) 83 { 84 if (ceil - start > 5000) 85 ceil = start + 5000; 86 Signal.checkQuit(); 87 } 88 int i = indexOf(b.getArray(), start, ceil, target); 89 if (i >= 0) 90 { 91 count--; 92 start = i + 1; 93 } 94 else 95 start = ceil; 96 } 97 if (start > b.gapEnd) 98 start -= b.gapEnd - b.gapStart; 99 return ((long) count << 32) | start; 100 } 101 else 102 { 103 while (start > limit && count < 0) 104 { 105 if (start == b.gapEnd) 106 start = b.gapStart; 107 int floor; 108 if (start <= b.gapStart || limit >= b.gapEnd) 109 floor = limit; 110 else 111 floor = b.gapEnd; 112 if (allowQuit) 113 { 114 if (start - floor > 5000) 115 floor = start - 5000; 116 Signal.checkQuit(); 117 } 118 int i = lastIndexOf(b.getArray(), start - 1, floor, target); 119 if (i >= 0) 120 { 121 count++; 122 start = i; 123 } 124 else 125 start = floor; 126 } 127 128 if (start >= b.gapEnd) 129 start -= b.gapEnd - b.gapStart; 130 if (count != 0) 131 return ((long) (- count) << 32) | start; 132 else 133 { 134 return start + 1; 138 } 139 } 140 } 141 } 142 | Popular Tags |