View Javadoc
1   package nom.tam.fits.utilities;
2   
3   /*
4    * #%L
5    * nom.tam FITS library
6    * %%
7    * Copyright (C) 1996 - 2024 nom-tam-fits
8    * %%
9    * This is free and unencumbered software released into the public domain.
10   *
11   * Anyone is free to copy, modify, publish, use, compile, sell, or
12   * distribute this software, either in source code form or as a compiled
13   * binary, for any purpose, commercial or non-commercial, and by any
14   * means.
15   *
16   * In jurisdictions that recognize copyright laws, the author or authors
17   * of this software dedicate any and all copyright interest in the
18   * software to the public domain. We make this dedication for the benefit
19   * of the public at large and to the detriment of our heirs and
20   * successors. We intend this dedication to be an overt act of
21   * relinquishment in perpetuity of all present and future rights to this
22   * software under copyright law.
23   *
24   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27   * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
28   * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
29   * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30   * OTHER DEALINGS IN THE SOFTWARE.
31   * #L%
32   */
33  
34  /**
35   * This class is a pointer into a part of an other string, it can be manipulated by changing the position pointers into
36   * the "original" string. This class is aware of the escape quote, two quotes in sequence the respresent a single quote.
37   *
38   * @author     Richard van Nieuwenhoven
39   *
40   * @deprecated (<i>for internal use</i>) Was used by {@link FitsLineAppender} only.
41   */
42  @Deprecated
43  public class FitsSubString {
44  
45      /**
46       * the length of the substring (starting at the offset).
47       */
48      private int length;
49  
50      /**
51       * the offset into the original string where this string starts.
52       */
53      private int offset;
54  
55      /**
56       * the original String.
57       */
58      private final String originalString;
59  
60      /**
61       * constructor for the substring, start by representing the whole string.
62       *
63       * @param originalString the string to represent.
64       */
65      public FitsSubString(String originalString) {
66          this.originalString = originalString == null ? "" : originalString;
67          offset = 0;
68          length = this.originalString.length();
69      }
70  
71      /**
72       * append the current string representation to the StringBuffer.
73       *
74       * @param buffer the buffer to append to.
75       */
76      public void appendTo(StringBuilder buffer) {
77          buffer.append(originalString, offset, offset + length);
78      }
79  
80      /**
81       * get the character at the specified position.
82       *
83       * @param  pos the position the get the character from
84       *
85       * @return     the character at the specified position
86       */
87      public char charAt(int pos) {
88          return originalString.charAt(pos + offset);
89      }
90  
91      /**
92       * @return get the length of the orginal string from the current offset.
93       */
94      public int fullLength() {
95          return originalString.length() - offset;
96      }
97  
98      /**
99       * check the string and set it to the maximum length specified. if a escaped quote is on the boundary the length is
100      * reduced in a way that the string does not separate an escape quote.
101      *
102      * @param max the maximum string legth to set.
103      */
104     public void getAdjustedLength(int max) {
105         if (max <= 0) {
106             length = 0;
107         } else if (length > max) {
108             int pos = max - 1;
109             while (charAt(pos) == '\'') {
110                 pos--;
111             }
112             // now we are at the start of the quotes step forward in steps of 2
113             pos += (max - 1 - pos) / 2 * 2;
114             length = pos + 1;
115         }
116     }
117 
118     /**
119      * @return the string length of this String.
120      */
121     public int length() {
122         return length;
123     }
124 
125     /**
126      * shift the sting to the rest of the string, the part of the original string that is after the part of the string
127      * this instance currently represents.
128      */
129     public void rest() {
130         offset += length;
131         length = originalString.length() - offset;
132     }
133 
134     /**
135      * skip over the specified number of characters.
136      *
137      * @param count the number of chars to skip
138      */
139     public void skip(int count) {
140         offset += count;
141         length -= count;
142     }
143 
144     /**
145      * @param  string the string to check
146      *
147      * @return        true if the current string starts with the specified string.
148      */
149     public boolean startsWith(String string) {
150         return originalString.regionMatches(offset, string, 0, string.length());
151     }
152 }