Subversion Repository Public Repository

litesoft

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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
package org.litesoft.core.typeutils;

import java.util.*;

import org.litesoft.core.annotations.*;
import org.litesoft.core.util.stringmatching.*;

public class Strings
{
    public static final String[] EMPTY_ARRAY = new String[0];

    private static final String SPACES_32 = "                                "; // 32 count-em! spaces

    /**
     * Adjust a String Array so that it is either null (error condition) or has the <code>DesiredLength</code>.
     * <p/>
     * Note: The <code>Source</code> (original) array may be return.
     *
     * @param pDesiredLength of the resulting array (must NOT be < 0)
     * @param pSource        array
     *
     * @return null if the <code>Source</code> is longer then the <code>DesiredLength</code>; otherwise
     *         an Array that is exactly <code>DesiredLength</code> long (padded with nulls).
     */
    public static @Nullable String[] expectArray( int pDesiredLength, @Nullable String[] pSource )
    {
        Integers.assertNonNegative( "DesiredLength", pDesiredLength );
        int zCurrentLength = (pSource == null) ? 0 : pSource.length;
        if ( pDesiredLength < zCurrentLength )
        {
            return null;
        }
        if ( pDesiredLength == zCurrentLength )
        {
            return pSource;
        }
        String[] rv = new String[pDesiredLength];
        if ( zCurrentLength != 0 )
        {
            System.arraycopy( pSource, 0, rv, 0, zCurrentLength );
        }
        return rv;
    }

    /**
     * Return an String Array made up of the end elements of <code>Strings</code>, but who's first element is
     * the <code>FromIndex</code> element of <code>Strings</code>.
     * <p/>
     * Note: It is OK for <code>Strings</code> to be too short (or even null).
     * Note: The <code>Strings</code> (original) array may be return.
     *
     * @param pStrings   array
     * @param pFromIndex is the index into <code>Strings</code> to start the resulting array from (must be >= 0).
     *
     * @return null if
     */
    public static @Nullable String[] everythingFrom( @Nullable String[] pStrings, int pFromIndex )
    {
        Integers.assertNonNegative( "FromIndex", pFromIndex );
        if ( (pStrings == null) || (pStrings.length <= pFromIndex) )
        {
            return EMPTY_ARRAY;
        }
        String[] rv = new String[pStrings.length - pFromIndex];
        System.arraycopy( pStrings, pFromIndex, rv, 0, rv.length );
        return rv;
    }

    public static String spaces( int pSpaces )
    {
        if ( pSpaces < 1 )
        {
            return "";
        }
        if ( pSpaces <= SPACES_32.length() )
        {
            return SPACES_32.substring( 0, pSpaces );
        }
        // Note use of StringBuilder as StringBuilder was added in 1.5
        StringBuilder retval = new StringBuilder( pSpaces );
        for ( int i = 0; i < pSpaces; i++ )
        {
            retval.append( ' ' );
        }
        return retval.toString();
    }

    public static String dupChars( char pCharToDup, int pDupCount )
    {
        if ( pDupCount < 1 )
        {
            return "";
        }
        StringBuilder sb = new StringBuilder( pDupCount );
        while ( pDupCount-- > 0 )
        {
            sb.append( pCharToDup );
        }
        return sb.toString();
    }

    public static String dup( String pToDup, int pDupCount )
    {
        if ( pToDup == null )
        {
            return null;
        }
        if ( pDupCount < 1 )
        {
            return "";
        }
        StringBuilder sb = new StringBuilder( pDupCount * pToDup.length() );
        while ( pDupCount-- > 0 )
        {
            sb.append( pToDup );
        }
        return sb.toString();
    }

    public static boolean contains( String pString, String pToFind )
    {
        return Objects.isNotNull( pString ) && isNotNullOrEmpty( pToFind ) && pString.contains( pToFind );
    }

    public static boolean contains( String pString, char pToFind )
    {
        return Objects.isNotNull( pString ) && (-1 != pString.indexOf( pToFind ));
    }

    public static String noEmpty( String pString )
    {
        return noEmpty( pString, null );
    }

    public static String noEmpty( String pString, String pDefault )
    {
        if ( pString != null )
        {
            pString = pString.trim();
            if ( pString.length() != 0 )
            {
                return pString;
            }
        }
        return pDefault;
    }

    public static boolean isBlank( String pLine )
    {
        if ( pLine != null )
        {
            for ( int at = pLine.length(); --at >= 0; )
            {
                if ( pLine.charAt( at ) != ' ' )
                {
                    return false;
                }
            }
        }
        return true;
    }

    public static boolean areEqualIgnoreCase( String a, String b )
    {
        // noinspection StringEquality
        return (a == b) || ((null != a) && a.equalsIgnoreCase( b ));
    }

    /**
     * Return if <tt>pThis</tt> starts with <tt>pStartsWith</tt> ignoring
     * the case of the strings
     * <p/>
     *
     * @param pThis       The String to check the front of
     * @param pStartsWith The String that <tt>pThis</tt>'s front must match
     *
     * @return <tt>true</tt> if <tt>pThis</tt> starts with <tt>pStartsWith</tt> ignoring
     *         the case
     */
    public static boolean startsWithIgnoreCase( String pThis, String pStartsWith )
    {
        if ( (pThis != null) && (pStartsWith != null) )
        {
            int lenStartsWith = pStartsWith.length();
            if ( lenStartsWith <= pThis.length() )
            {
                return pStartsWith.equalsIgnoreCase( pThis.substring( 0, lenStartsWith ) );
            }
        }
        return false;
    }

    public static String deEmpty( String pString, String pDefault )
    {
        pString = noEmpty( pString );
        return (pString != null) ? pString : pDefault;
    }

    public static String[] noEmpty( String[] pStrings )
    {
        return (pStrings == null || pStrings.length == 0) ? null : pStrings;
    }

    public static String[] noEmpties( String[] pStrings )
    {
        if ( pStrings != null )
        {
            for ( int i = pStrings.length; --i >= 0; )
            {
                String zString = noEmpty( pStrings[i] );
                if ( zString == null )
                {
                    pStrings = removeStringFromArray( pStrings, i );
                }
                pStrings[i] = zString;
            }
            if ( pStrings.length == 0 )
            {
                pStrings = null;
            }
        }
        return pStrings;
    }

    public static String[] deNull( String[] pStrings )
    {
        return (pStrings != null) ? pStrings : EMPTY_ARRAY;
    }

    public static String deNull( CharSequence pString )
    {
        return deNull( pString, "" );
    }

    public static String deNull( CharSequence pString, String pDefault )
    {
        return (pString != null) ? pString.toString() : pDefault;
    }

    public static boolean isNotNullOrEmpty( String pStringToCheck )
    {
        return ((pStringToCheck != null) && (pStringToCheck.trim().length() != 0));
    }

    public static boolean allEmpty( String[] pParts )
    {
        if ( pParts != null )
        {
            for ( String part : pParts )
            {
                if ( isNotNullOrEmpty( part ) )
                {
                    return false;
                }
            }
        }
        return true;
    }

    public static boolean isNullOrEmptyOrSpaces( String pStringToCheck )
    {
        return ((pStringToCheck == null) || //
                (pStringToCheck.length() == 0) || //
                (-1 != pStringToCheck.indexOf( ' ' )));
    }

    public static boolean isNullOrEmpty( String pStringToCheck )
    {
        return ((pStringToCheck == null) || (pStringToCheck.trim().length() == 0));
    }

    public static String trimLeadingSpaces( String pStr )
    {
        if ( (pStr == null) || !pStr.startsWith( " " ) )
        {
            return pStr;
        }
        int sLen = pStr.length();
        for ( int i = 0; i < sLen; i++ )
        {
            if ( pStr.charAt( i ) != ' ' )
            {
                return pStr.substring( i );
            }
        }
        return "";
    }

    public static String trimTrailingSpaces( String pStr )
    {
        if ( (pStr == null) || !pStr.endsWith( " " ) )
        {
            return pStr;
        }
        int sLen = pStr.length();
        for ( int i = sLen - 1; --i >= 0; )
        {
            if ( pStr.charAt( i ) != ' ' )
            {
                return pStr.substring( 0, i + 1 );
            }
        }
        return "";
    }

    public static String[] parseChar( String pStringToParse, char pSeparator )
    {
        if ( isNullOrEmpty( pStringToParse ) )
        {
            return new String[0];
        }
        int count = 1;
        int from = 0;
        for ( int at; -1 != (at = pStringToParse.indexOf( pSeparator, from )); from = at + 1 )
        {
            count++;
        }
        String[] parts = new String[count];
        count = from = 0;
        for ( int at; -1 != (at = pStringToParse.indexOf( pSeparator, from )); from = at + 1 )
        {
            parts[count++] = pStringToParse.substring( from, at );
        }
        parts[count] = pStringToParse.substring( from );
        return parts;
    }

    public static String replace( String pSource, char pToFind, String pToReplaceWith )
    {
        if ( (pSource != null) && (pToReplaceWith != null) )
        {
            int removeFor = 1;
            int adjustFromBy = pToReplaceWith.length();
            int from = 0;
            for ( int at; -1 != (at = pSource.indexOf( pToFind, from )); from = at + adjustFromBy )
            {
                pSource = pSource.substring( 0, at ) + pToReplaceWith + pSource.substring( at + removeFor );
            }
        }
        return pSource;
    }

    public static String replace( String pSource, String pToFind, String pToReplaceWith )
    {
        if ( (pSource != null) && (pToFind != null) && (pToReplaceWith != null) )
        {
            int removeFor = pToFind.length();
            int adjustFromBy = pToReplaceWith.length();
            int from = 0;
            for ( int at; -1 != (at = pSource.indexOf( pToFind, from )); from = at + adjustFromBy )
            {
                pSource = pSource.substring( 0, at ) + pToReplaceWith + pSource.substring( at + removeFor );
            }
        }
        return pSource;
    }

    public static String defaultIfNull( String pTestString, String pDefault )
    {
        return isNullOrEmpty( pTestString ) ? pDefault : pTestString;
    }

    public static String normalizeNewLines( String pString )
    {
        pString = replace( pString, "\r\n", "\n" );
        pString = replace( pString, "\n\r", "\n" );
        pString = replace( pString, '\r', "\n" );

        return pString;
    }

    public static String[] stringToLines( String pString )
    {
        return parseChar( replace( normalizeNewLines( pString ), "\f", "\n" ), '\n' );
    }

    public static String linesToString( String[] pLines )
    {
        StringBuilder sb = new StringBuilder();
        if ( pLines != null )
        {
            for ( String zLine : pLines )
            {
                sb.append( zLine );
                sb.append( '\n' );
            }
        }
        return sb.toString();
    }

    public static String makeNonBlankLines( String pSource, int pLinesToMake )
    {
        pSource = normalizeNewLines( deNull( pSource ) );
        StringBuilder sb = new StringBuilder();
        for ( int i = 0; i < pLinesToMake; i++ )
        {
            int at = (pSource += '\n').indexOf( '\n' );
            String line = pSource.substring( 0, at );
            pSource = pSource.substring( at + 1 );
            if ( sb.length() != 0 )
            {
                sb.append( '\n' );
            }
            sb.append( line ).append( ' ' );
        }
        return sb.toString();
    }

    public static String nullOKtoString( Object value )
    {
        return (value == null) ? null : value.toString();
    }

    public static String nullToEmptytoString( Object value )
    {
        return (value == null) ? "" : value.toString();
    }

    public static void errorNullOrEmptyOrSpace( String pErrorMessage, String pForm )
            throws IllegalArgumentException
    {
        error( pForm, pErrorMessage, " not allowed to be null or empty or have any spaces" );
    }

    public static void errorNullOrEmpty( String pErrorMessage, String pForm )
            throws IllegalArgumentException
    {
        error( pForm, pErrorMessage, " not allowed to be null or empty" );
    }

    public static void error( String pForm, String pErrorMessage, String pMessagePlus )
            throws IllegalArgumentException
    {
        if ( isNullOrEmpty( pErrorMessage ) )
        {
            pErrorMessage = pForm;
        }
        if ( -1 != pErrorMessage.indexOf( ' ' ) )
        {
            throw new IllegalArgumentException( pErrorMessage );
        }
        throw new IllegalArgumentException( pErrorMessage + pMessagePlus );
    }

    public static String padIt( int pMinDesiredLength, String pIt )
    {
        String rv = deNull( pIt );
        int padBy = pMinDesiredLength - rv.length();
        return (padBy <= 0) ? rv : (spaces( padBy ) + rv);
    }

    public static String iTpad( String pIt, int pMinDesiredLength )
    {
        String rv = deNull( pIt );
        int padBy = pMinDesiredLength - rv.length();
        return (padBy <= 0) ? rv : (rv + spaces( padBy ));
    }

    public static String noSpaces( String pSource )
    {
        if ( (pSource == null) || (pSource.length() == 0) || (pSource.indexOf( ' ' ) == -1) )
        {
            return pSource;
        }
        StringBuilder sb = new StringBuilder( pSource );
        for ( int i = pSource.length(); --i >= 0; )
        {
            if ( sb.charAt( i ) == ' ' )
            {
                sb.deleteCharAt( i );
            }
        }
        return sb.toString();
    }

    public static boolean containsAnyOf( String pToCheck, String pCharsToCheckFor )
    {
        if ( (pToCheck != null) && (pToCheck.length() > 0) && //
             (pCharsToCheckFor != null) && (pCharsToCheckFor.length() > 0) )
        {
            for ( int i = 0; i < pCharsToCheckFor.length(); i++ )
            {
                if ( -1 != pToCheck.indexOf( pCharsToCheckFor.charAt( i ) ) )
                {
                    return true;
                }
            }
        }
        return false;
    }

    public static String getCommonTail( String pStr1, String pStr2 )
    {
        // Binary Search
        int goodTailLength = 0;
        for ( int potentialLength = Math.min( pStr1.length(), pStr2.length() ); potentialLength > goodTailLength; )
        {
            int mid = (goodTailLength + potentialLength + 1) / 2;
            String tail = pStr1.substring( pStr1.length() - mid );
            if ( pStr2.endsWith( tail ) )
            {
                goodTailLength = mid;
            }
            else
            {
                potentialLength = mid - 1;
            }
        }
        return (goodTailLength == 0) ? "" : pStr1.substring( pStr1.length() - goodTailLength );
    }

    public static int indexOfOrLength( String pToSearch, char pToFind )
    {
        return helperIndexOfOrLength( pToSearch, pToSearch.indexOf( pToFind ) );
    }

    public static int indexOfOrLength( String pToSearch, String pToFind )
    {
        return helperIndexOfOrLength( pToSearch, pToSearch.indexOf( pToFind ) );
    }

    private static int helperIndexOfOrLength( String pToSearch, int pAt )
    {
        return (pAt != -1) ? pAt : pToSearch.length();
    }

    public static String[] removeStringFromArray( String[] pArray, int pIndexToRemove )
    {
        if ( Objects.isNullOrEmpty( pArray ) || (pIndexToRemove < 0) || (pArray.length <= pIndexToRemove) )
        {
            return pArray;
        }
        int zNewLength = pArray.length - 1;
        String[] zNewArray = new String[zNewLength];
        if ( pIndexToRemove != 0 )
        {
            System.arraycopy( pArray, 0, zNewArray, 0, pIndexToRemove );
        }
        if ( pIndexToRemove != zNewLength )
        {
            System.arraycopy( pArray, pIndexToRemove + 1, zNewArray, pIndexToRemove, zNewLength - pIndexToRemove );
        }
        return zNewArray;
    }

    public static String[] appendStringArrays( String[] pArray1, String[] pArray2 )
    {
        if ( Objects.isNullOrEmpty( pArray2 ) )
        {
            return pArray1;
        }
        if ( Objects.isNullOrEmpty( pArray1 ) )
        {
            return pArray2;
        }
        String[] joined = new String[pArray1.length + pArray2.length];
        System.arraycopy( pArray1, 0, joined, 0, pArray1.length );
        System.arraycopy( pArray2, 0, joined, pArray1.length, pArray2.length );
        return joined;
    }

    public static String[] prependString( String pNewFirst, String[] pTheRest )
    {
        return appendStringArrays( new String[]{pNewFirst}, pTheRest );
    }

    public static String[] appendString( String[] pCurArray, String pNewLast )
    {
        return appendStringArrays( pCurArray, new String[]{pNewLast} );
    }

    /**
     * Is the string 'pInQuestion' made up of the 'pParts' where if only 1, then must be equal, otherwise
     * the pInQuestion.startsWith(first) && pInQuestion.endsWith(last) && the middle must be found in
     * order with no overlap between.
     *
     * @param pInQuestion - null will always return false
     * @param pParts      - Null treated as empty & Null Elements treated as ""
     */
    public static boolean isMadeUpFromParts( String pInQuestion, String[] pParts )
    {
        StringMatcher zSM = StringMatcherFactory.createEquals( pParts );
        return (zSM != null) && zSM.matches( pInQuestion );
    }

    public static String[] removeAllBlankOrCommentLines( String[] pLines )
    {
        List<String> lines = new LinkedList<String>();

        for ( String line : deNull( pLines ) )
        {
            String s = line.trim();
            if ( (s.length() != 0) && !(s.startsWith( "#" ) || s.startsWith( "//" )) )
            {
                lines.add( line );
            }
        }
        return lines.toArray( new String[lines.size()] );
    }

    public static String merge( String[] pLines )
    {
        if ( (pLines == null) || (pLines.length == 0) )
        {
            return "";
        }
        if ( pLines.length == 1 )
        {
            return pLines[0];
        }
        int length = 0;
        for ( String line : pLines )
        {
            if ( line != null )
            {
                length += line.length() + 2;
            }
        }
        StringBuilder sb = new StringBuilder( length );
        for ( String line : pLines )
        {
            if ( line != null )
            {
                sb.append( line ).append( '\r' ).append( '\n' );
            }
        }
        return sb.toString();
    }

    public static boolean isAllUppercaseOrSpaces( String pToTest )
    {
        if ( isNotNullOrEmpty( pToTest ) )
        {
            for ( int i = 0; i < pToTest.length(); i++ )
            {
                char c = pToTest.charAt( i );
                if ( (c != ' ') && !Character.isUpperCase( c ) )
                {
                    return false;
                }
            }
        }
        return true;
    }

    public static boolean isAllUppercase( String pToTest )
    {
        if ( isNotNullOrEmpty( pToTest ) )
        {
            for ( int i = 0; i < pToTest.length(); i++ )
            {
                if ( !Character.isUpperCase( pToTest.charAt( i ) ) )
                {
                    return false;
                }
            }
        }
        return true;
    }
}

Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/core/typeutils/Strings.java

Diff revisions: vs.
Revision Author Commited Message
811 Diff Diff GeorgeS picture GeorgeS Sat 18 Aug, 2012 13:45:18 +0000
810 Diff Diff GeorgeS picture GeorgeS Thu 16 Aug, 2012 04:16:07 +0000
809 Diff Diff GeorgeS picture GeorgeS Thu 16 Aug, 2012 04:10:46 +0000
806 Diff Diff GeorgeS picture GeorgeS Thu 16 Aug, 2012 03:48:22 +0000
804 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 12:48:51 +0000
803 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 04:08:34 +0000
802 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 04:04:47 +0000
801 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 03:59:02 +0000
799 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 03:32:04 +0000
787 GeorgeS picture GeorgeS Mon 30 Jul, 2012 03:00:12 +0000