/plugins/XSearch/tags/release-1-6/xsearch/CharIndexedSegment.java

# · Java · 97 lines · 43 code · 10 blank · 44 comment · 4 complexity · c514d840670872712823ab659ce6897b MD5 · raw file

  1. /*
  2. * CharIndexedSegment.java
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1998 Wes Biggs
  7. * Copyright (C) 2000, 2001 Slava Pestov
  8. *
  9. * This library is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Library General Public License as published
  11. * by the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Library General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Library General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. //package org.gjt.sp.jedit.search;
  24. package xsearch;
  25. //{{{ Imports
  26. import java.io.Serializable;
  27. import javax.swing.text.Segment;
  28. import gnu.regexp.*;
  29. //}}}
  30. /**
  31. * Lets <code>gnu.regexp</code> search within <code>Segment</code> objects.
  32. */
  33. public class CharIndexedSegment implements CharIndexed, Serializable
  34. {
  35. //{{{ CharIndexedSegment constructor
  36. /**
  37. * Creates a new <code>CharIndexedSegment</code>.
  38. * @since jEdit 4.1pre3
  39. */
  40. public CharIndexedSegment(Segment seg, int index)
  41. {
  42. this.seg = seg;
  43. m_index = index;
  44. } //}}}
  45. //{{{ CharIndexedSegment constructor
  46. /**
  47. * Creates a new <code>CharIndexedSegment</code>.
  48. * @since jEdit 4.1pre1
  49. */
  50. public CharIndexedSegment(Segment seg, boolean reverse)
  51. {
  52. this.seg = seg;
  53. m_index = (reverse ? seg.count - 1 : 0);
  54. this.reverse = reverse;
  55. } //}}}
  56. //{{{ charAt() method
  57. public char charAt(int index)
  58. {
  59. if(reverse)
  60. index = -index;
  61. return ((m_index + index) < seg.count && m_index + index >= 0)
  62. ? seg.array[seg.offset + m_index + index]
  63. : CharIndexed.OUT_OF_BOUNDS;
  64. } //}}}
  65. //{{{ isValid() method
  66. public boolean isValid()
  67. {
  68. return (m_index >=0 && m_index < seg.count);
  69. } //}}}
  70. //{{{ reset() method
  71. public void reset()
  72. {
  73. m_index = (reverse ? seg.count - 1 : 0);
  74. } //}}}
  75. //{{{ move() method
  76. public boolean move(int index)
  77. {
  78. if(reverse)
  79. index = -index;
  80. return ((m_index += index) < seg.count);
  81. } //}}}
  82. //{{{ Private members
  83. private Segment seg;
  84. private int m_index;
  85. private boolean reverse;
  86. //}}}
  87. }