1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package Torello.BuildJAR.Generators.CodeGen;

import Torello.Java.StringParse;
import Torello.Java.FileRW;

import java.io.IOException;

public class IndexOfCodeGen
{
    private static final String[] methods =
    { "all", "first", "last", "nth", "nthFromEnd", "left", "nthLeft" };

    private static final boolean[] BOOLS = { true, false };

    private static final String I4   = "    ";
    private static final String I8   = I4 + I4;
    private static final String I12  = I4 + I4 + I4;

    public static void main(String[] argv) throws IOException
    { 
        StringBuffer sb = new StringBuffer();

        sb.append(
            "package Torello.Java;\n\n" +
            "import java.util.Vector;\n" +
            "import java.util.stream.IntStream;\n\n" +
            "/**\n" +
            " * This class builds upon the API that is provided by {@code String indexOf} to " +
                "provide a\n" +
            " * larger collection of search routines for Java {@code String's}.\n" +
            " * \n" +
            " * <EMBED CLASS='external-html' DATA-FILE-ID=STR_INDEX_OF>\n" +
            " */\n" +
            "@Torello.JavaDoc.StaticFunctional\n" +
            "@Torello.JavaDoc.JDHeaderBackgroundImg\n" +
            "public class StrIndexOf\n" +
            "{\n" +
            "    private StrIndexOf() { }\n\n"
        );

        for (String method : methods)
            for (boolean caseInsensitive : BOOLS)
                for (boolean subRange : BOOLS)
                    for (boolean charOrString : BOOLS)
                        if (subRange && (method.equals("left") || method.equals("nthLeft")))
                            continue;
                        else
                            sb  .append(javaDoc(method, caseInsensitive, subRange, charOrString))
                                .append(signature(method, caseInsensitive, subRange, charOrString))
                                .append(body(method, caseInsensitive, subRange, charOrString));

        sb.append("}");

        FileRW.writeFile(sb.toString(), "StrIndexOf.java");
    }


    private static String javaDoc
        (String method, boolean caseInsensitive, boolean subRange, boolean charOrString)
    {
        String ret = I4 + "/**\n";

        String cOrS = charOrString
            ? "character {@code 'c'}"
            : "substring {@code 'cmpStr'}";

        String retInt =
            '\n' +
            I4 + " * in {@code 'srcStr'}, and return the integer index-location into " +
                "{@code 'srcStr'} where the\n" +
            I4 + " * match occured.\n";

        String retArr =
            '\n' +
            I4 + " * in {@code 'srcStr'}, and return an integer-array of index-locations into " +
                "{@code 'srcStr.'}\n";

        switch (method)
        {
            case "first": ret +=
                I4 + " * Search for the <B STYLE='color: red'>first</B> occurence of " + cOrS +
                    retInt;

                break;

            case "last": ret +=
                I4 + " * Search for the <B STYLE='color: red'>last</B> occurence of " + cOrS +
                    retInt;

                break;

            case "all": ret +=
                I4 + " * Search for <B STYLE='color: red'>all</B> occurences of " + cOrS +
                    retArr;

                break;

            case "nth":
                ret +=  I4 + " * Search for the <B STYLE='color: red'>nth</B> occurence of " + cOrS +
                    retInt;

                break;

            case "nthFromEnd": ret +=
                I4 + " * Search for the <B STYLE='color: red'>nth</B> occurence of " + cOrS +
                    ", but\n" +
                I4 + " * start the search with the <I>last character of the String and " +
                    "work\n" +
                I4 + " * <B STYLE='color: red'>left</B>.</I>  Return the integer " +
                    "index-location into {@code 'srcStr'}\n" +
                I4 + " * where the match occured.\n";

                break;

            case "left": ret +=
                I4 + " * Search for the <B STYLE='color: red'>first</B> occurence of " +
                    cOrS + '\n' +
                I4 + " * , but <I>search <B STYLE='color: red'>left</B> beginning at " +
                    "{@code 'srcStr'} position\n" +
                I4 + " * {@code 'fromIndex'}</I>.\n" +
                retInt;
                break;

            case "nthLeft": ret +=
                I4 + " * Search for the <B STYLE='color: red'>nth</B> occurence of " + cOrS +
                    " in\n" +
                I4 + " * {@code 'srcStr'}, but <I>begin the search at index-position " +
                    "{@code 'fromIndex'}\n" +
                I4 + " * and work <B STYLE='color: red'>left</B>.</I>  Return the integer " +
                    "index-location into\n" +
                I4 + " * {@code 'srcStr'} where the match occured.\n";

                break;

            default: throw new Torello.Java.UnreachableError();
        }

        if (caseInsensitive) ret +=
            I4 + " * \n" +
            I4 + " * <BR /><BR />The comparisons performed " +
                "<B STYLE='color: red'>** are not **</B> case-sensitive\n";

        else ret +=
            I4 + " * \n" +
            I4 + " * <BR /><BR />The comparisons performed " +
                "<B STYLE='color: red'>** are **</B> case-sensitive\n";

        if (subRange) ret +=
            I4 + " * \n" +
            I4 + " * <EMBED CLASS='external-html' DATA-FILE-ID=STR_SUBSECT>\n";

        ret += 
            I4 + " * \n" +
            I4 + " * @param srcStr This may be any {@code java.lang.String}.  " +
                "It's contents will be searched\n";

        if (method.equals("all")) ret +=
            I4 + " * for the specified matches, and an integer-array of {@code String}-index " +
                    "pointers\n" +
            I4 + " * will be returned.\n";

        else ret +=
            I4 + " * for the specified matches, and an index-pointer as an integer will be " +
                "returned.\n";

        if (subRange) ret +=
            I4 + " * \n" +
            I4 + " * @param sPos <EMBED CLASS='external-html' DATA-FILE-ID=SPOSSTR>\n" +
            I4 + " * @param ePos <EMBED CLASS='external-html' DATA-FILE-ID=EPOSSTR>\n";

        if (method.equals("left") || method.equals("nthLeft")) ret +=
            I4 + " * \n" +
            I4 + " * @param fromIndex This is the right-most index-position in " +
                "{@code 'srcStr'} (*INCLUSIVE*)\n" +
            I4 + " * from where the search shall start.\n";

        if (method.startsWith("nth")) ret +=
            I4 + " * \n" +
            I4 + " * @param n This is the number of matches to skip when searching for " +
                cOrS + "\n     * before returning an answer.\n";

        ret += I4 + " * \n";

        if (charOrString) ret +=
            I4 + " * @param c This is the character that will be 'searched-for' " +
                "in input-parameter\n" +
            I4 + " * {@code String 'srcStr'}.\n";

        else ret +=
            I4 + " * @param cmpStr This is the sub-string that will be 'searched-for' " +
                "in input parameter\n" +
            I4 + " * {@code String 'srcStr'}.\n";

        ret += I4 + " * \n";

        switch (method)
        {
            case "first": ret +=
                I4 + " * @return The {@code String} index of the <B STYLE='color: red'>" +
                    "first</B> identified\n" +
                I4 + " * sub-string match in {@code 'srcStr'}\n";

                break;

            case "last": ret +=
                I4 + " * @return The {@code String} index of the <B STYLE='color: red'>" +
                    "last</B> identified\n" +
                I4 + " * sub-string match in {@code 'srcStr'}\n";

                break;

            case "all": ret +=
                I4 + " * @return An integer-pointer array of <B STYLE='color: red'>all</B>\n" +
                I4 + " * {@code java.lang.String}-indices where matches were found.\n";

                break;

            case "nth": ret +=
                I4 + " * @return The {@code String} index of the <B STYLE='color: red'>" +
                    "nth</B> identified sub-string\n" +
                I4 + " * match in {@code 'srcStr'}\n";

                break;

            case "nthFromEnd": ret +=
                I4 + " * @return The {@code String} index of the <B STYLE='color: red'>" +
                    "nth</B> identified sub-string\n" +
                I4 + " * match in {@code 'srcStr'}, <I>counting <B STYLE='color: red'>" +
                    "left</B> from the end of\n" +
                I4 + " * {@code 'srcStr'}</I>\n";

                break;

            case "left": ret +=
                I4 + " * @return The {@code String} index of the <B STYLE='color: red'>" +
                    "first</B> identified\n" +
                I4 + " * sub-string match in {@code 'srcStr'}, <I>counting " +
                    "<B STYLE='color: red'>left</B>\n" +
                I4 + " * in {@code 'srcStr'} from position {@code 'fromIndex'}</I>\n";

                break;

            case "nthLeft": ret +=
                I4 + " * @return The {@code String} index of the <B STYLE='color: red'>" +
                    "nth</B> identified sub-string\n" +
                I4 + " * match in {@code 'srcStr'}, <I>counting <B STYLE='color: red'>" +
                    "left</B> in {@code 'srcStr'}\n" +
                I4 + " * from position {@code 'fromIndex'}</I>\n";

                break;

            default: throw new Torello.Java.UnreachableError();
        }

        if (subRange) ret +=
            I4 + " * \n" +
            I4 + " * @throws StringIndexOutOfBoundsException\n" +
            I4 + " * <EMBED CLASS='external-html' DATA-FILE-ID=SIOOB_EX>\n";

        if (method.equals("nth") || method.equals("nthFromEnd") || method.equals("nthLeft")) ret +=
            I4 + " * \n" +
            I4 + " * @throws NException If the value of {@code 'n'} is negative, or greater " +
                "than\n" +
            I4 + " * {@code srcStr.length()}, this exception shall throw.\n";

        ret += I4 + " */\n";
        return ret;
    }


    private static String signature
        (String method, boolean caseInsensitive, boolean subRange, boolean charOrString)
    {
        String ret = 
            I4 + "public static " +
                (method.equals("all") ? "int[] " : "int ") +
                method +
                (caseInsensitive ? "_CI" : "") +
                "(String srcStr, " +
                (subRange ? "int sPos, int ePos, " : "");

        switch (method)
        {
            case "first":                                           break;
            case "last":                                            break;
            case "all":                                             break;
            case "nth":         ret += "int n, ";                   break;
            case "nthFromEnd":  ret += "int n, ";                   break;
            case "left":        ret += "int fromIndex, ";           break;
            case "nthLeft":     ret += "int fromIndex, int n, ";    break;

            default: throw new Torello.Java.UnreachableError();
        }

        if (charOrString)   ret += "char c)\n";
        else                ret += "String cmpStr)\n";

        return ret;
    }

    private static String body
        (String method, boolean caseInsensitive, boolean subRange, boolean charOrString)
    {
        String ret = I4 + "{\n";

        if (method.equals("nth") || method.equals("nthFromEnd") | method.equals("nthLeft"))
            ret += I8 + "NException.check(n, srcStr);\n\n";

        int varTypeSpacing;

        if (method.equals("all"))   varTypeSpacing = "IntStream.Builder   ".length();
        else if (! charOrString)    varTypeSpacing = "int ".length();
        else                        varTypeSpacing = "LV ".length();

        int varNameSpacing;

        if (! charOrString) varNameSpacing = "cmpStrLen   ".length();
        else                varNameSpacing = "l ".length(); // Same / can use "b ".length();

        if (method.equals("left") || method.equals("nthLeft")) ret +=
            I8 + StringParse.rightSpacePad("LV ", varTypeSpacing) +
                StringParse.rightSpacePad("l", varNameSpacing) + "= new LV(srcStr, 0, fromIndex";

        else ret += subRange 
            ?   I8 + StringParse.rightSpacePad("LV", varTypeSpacing) +
                    StringParse.rightSpacePad("l", varNameSpacing) + "= new LV(srcStr, sPos, ePos"
            :   I8 + StringParse.rightSpacePad("LV", varTypeSpacing) +
                    StringParse.rightSpacePad("l", varNameSpacing) + "= new LV(srcStr, 0, -1";

        ret += charOrString
            ? ");\n"
            : ", cmpStr.length());\n";

        if (method.equals("all")) ret +=
            I8 + StringParse.rightSpacePad("IntStream.Builder", varTypeSpacing) +
                StringParse.rightSpacePad("b", varNameSpacing) + "= IntStream.builder();\n";

        if (! charOrString) ret +=
            I8 + StringParse.rightSpacePad("int", varTypeSpacing) +
                StringParse.rightSpacePad("cmpStrLen", varNameSpacing) + "= cmpStr.length();\n";

        if (charOrString && caseInsensitive)
            ret += '\n' + I8 + "c = Character.toLowerCase(c);\n";

        // THESE TWO METHODS ARE **EXCLUSIVE** TOWARDS 'l.end'
        if (method.equals("last") || method.equals("nthFromEnd"))
            ret += '\n' + I8 + "for (int i=(l.end-1); i >= l.start; i--)\n";

        // THESE TWO METHODS ARE **INCLUSIVE** TOWARDS 'l.end'
        else if (method.equals("left") || method.equals("nthLeft"))
            ret += '\n' + I8 + "for (int i=l.end; i >= 0; i--)\n";

        // OTHER METHODS ARE ALL "Same old same old"
        else
            ret += '\n' + I8 + "for (int i=l.start; i < l.end; i++)\n";


        if (charOrString && caseInsensitive) ret +=
            I12 + "if (c == Character.toLowerCase(srcStr.charAt(i))) ";

        else if (charOrString) ret +=
            I12 + "if (c == srcStr.charAt(i)) ";

        else if (caseInsensitive) ret +=
            I12 + "if (srcStr.regionMatches(true, i, cmpStr, 0, cmpStrLen)) ";

        else ret +=
            I12 + "if (srcStr.regionMatches(i, cmpStr, 0, cmpStrLen)) ";


        if (method.equals("nth") || method.equals("nthLeft") || method.equals("nthFromEnd"))
            ret += "if (--n == 0) ";

        if (method.equals("all"))   ret += "b.accept(i);\n";
        else                        ret += "return i;\n";

        if (method.equals("all"))   ret += '\n' + I8 + "return b.build().toArray();\n";
        else                        ret += '\n' + I8 + "return -1;\n";
    

        return ret + I4 + "}\n\n";
    }
}