PageRenderTime 46ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/util/CharIndexedSegment.java

#
Java | 96 lines | 43 code | 10 blank | 43 comment | 4 complexity | 92cddf964da22163163579e24d6caacf MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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.util;
  24. //{{{ Imports
  25. import java.io.Serializable;
  26. import javax.swing.text.Segment;
  27. import gnu.regexp.*;
  28. //}}}
  29. /**
  30. * Lets <code>gnu.regexp</code> search within <code>Segment</code> objects.
  31. */
  32. public class CharIndexedSegment implements CharIndexed, Serializable
  33. {
  34. //{{{ CharIndexedSegment constructor
  35. /**
  36. * Creates a new <code>CharIndexedSegment</code>.
  37. * @since jEdit 4.1pre3
  38. */
  39. public CharIndexedSegment(Segment seg, int index)
  40. {
  41. this.seg = seg;
  42. m_index = index;
  43. } //}}}
  44. //{{{ CharIndexedSegment constructor
  45. /**
  46. * Creates a new <code>CharIndexedSegment</code>.
  47. * @since jEdit 4.1pre1
  48. */
  49. public CharIndexedSegment(Segment seg, boolean reverse)
  50. {
  51. this.seg = seg;
  52. m_index = (reverse ? seg.count - 1 : 0);
  53. this.reverse = reverse;
  54. } //}}}
  55. //{{{ charAt() method
  56. public char charAt(int index)
  57. {
  58. if(reverse)
  59. index = -index;
  60. return ((m_index + index) < seg.count && m_index + index >= 0)
  61. ? seg.array[seg.offset + m_index + index]
  62. : CharIndexed.OUT_OF_BOUNDS;
  63. } //}}}
  64. //{{{ isValid() method
  65. public boolean isValid()
  66. {
  67. return (m_index >=0 && m_index < seg.count);
  68. } //}}}
  69. //{{{ reset() method
  70. public void reset()
  71. {
  72. m_index = (reverse ? seg.count - 1 : 0);
  73. } //}}}
  74. //{{{ move() method
  75. public boolean move(int index)
  76. {
  77. if(reverse)
  78. index = -index;
  79. return ((m_index += index) < seg.count);
  80. } //}}}
  81. //{{{ Private members
  82. private Segment seg;
  83. private int m_index;
  84. private boolean reverse;
  85. //}}}
  86. }