/plugins/Code2HTML/branches/akaplan/LineTabExpander.java

# · Java · 81 lines · 50 code · 13 blank · 18 comment · 8 complexity · 0160c6b74f047d7d2ad400a5ef178787 MD5 · raw file

  1. /*
  2. * LineTabExpander.java
  3. * Copyright (c) 2000 Andre Kaplan
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. import org.gjt.sp.util.Log;
  20. public class LineTabExpander {
  21. private int tabSize;
  22. private int pos;
  23. private char[] spacer;
  24. public LineTabExpander() {
  25. this(4);
  26. }
  27. public LineTabExpander(int tabSize) {
  28. if (tabSize > 0) {
  29. this.tabSize = tabSize;
  30. }
  31. this.pos = 0;
  32. this.spacer = new char[this.tabSize];
  33. for (int i = 0; i < this.tabSize; i++) {
  34. this.spacer[i] = ' ';
  35. }
  36. }
  37. public String expand(char[] str, int strOff, int strLen) {
  38. StringBuffer buf = new StringBuffer();
  39. int off = strOff;
  40. int len = 0;
  41. char c;
  42. for (int i = 0; i < strLen; i++) {
  43. c = str[strOff + i];
  44. if (c != '\t') {
  45. len++;
  46. this.pos++;
  47. } else {
  48. int rem = (this.pos % this.tabSize);
  49. if (rem == 0) { rem = this.tabSize; }
  50. buf.append(str, off, len).append(this.spacer, 0, rem);
  51. off += len + 1;
  52. len = 0;
  53. this.pos += rem;
  54. }
  55. }
  56. buf.append(str, off, len);
  57. return buf.toString();
  58. }
  59. public String expand(String s) {
  60. return this.expand(s.toCharArray(), 0, s.length());
  61. }
  62. public int getPos() {
  63. return this.pos;
  64. }
  65. public void resetPos() {
  66. this.pos = 0;
  67. }
  68. }