~ubuntu-branches/ubuntu/jaunty/beagle/jaunty-security

« back to all changes in this revision

Viewing changes to Util/StringFu.cs

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Ebner
  • Date: 2008-05-04 00:31:32 UTC
  • mfrom: (1.1.21 upstream)
  • Revision ID: james.westby@ubuntu.com-20080504003132-2tkm5o8moo5952ri
Tags: 0.3.7-2ubuntu1
 * Merge from Debian unstable. (LP: #225746) Remaining Ubuntu changes:
  - debian/control:
    + Rename ice{weasel,dove}-beagle to {mozilla,thunderbird}-beagle and
      and update the dependencies accordingly.
    + Change Maintainer to Ubuntu Mono Team.
  - debian/rules:
    + Install the mozilla-beagle and thunderbird-beagle extensions.
  - ice{dove,weasel}.dirs:
    + Renamed to {mozilla,thunderbird}-beagle.dirs.
    + Fixed paths to point to usr/lib/{firefox,thunderbird}

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 
27
27
using System;
28
28
using System.Collections;
 
29
using System.Collections.Generic;
29
30
using System.Globalization;
30
31
using System.IO;
31
32
using System.Text;
47
48
                public static DateTime MinValueUtc = new DateTime (0, DateTimeKind.Utc);
48
49
                public static DateTime MaxValueUtc = new DateTime (DateTime.MaxValue.Ticks, DateTimeKind.Utc);
49
50
 
50
 
                // We use this instead of DateTime.ToUniversalTime() because
51
 
                // we want to assume DateTimeKind.Unspecified dates are UTC
 
51
                /// <summary>
 
52
                /// We use this instead of DateTime.ToUniversalTime() because
 
53
                /// we want to assume DateTimeKind.Unspecified dates are UTC
 
54
                /// </summary>
 
55
                /// <param name="dt">
 
56
                /// A <see cref="DateTime"/>
 
57
                /// </param>
 
58
                /// <returns>
 
59
                /// A <see cref="DateTime"/>
 
60
                /// </returns>
52
61
                static private DateTime ToUniversalTime (DateTime dt)
53
62
                {
54
63
                        switch (dt.Kind) {
227
236
                        return String.Format (Catalog.GetString ("{0:0.0} MB"), len/(double)oneMb);
228
237
                }
229
238
 
230
 
                // Here we:
231
 
                // (1) Replace non-alphanumeric characters with spaces
232
 
                // (2) Inject whitespace between lowercase-to-uppercase
233
 
                //     transitions (so "FooBar" becomes "Foo Bar")
234
 
                //     and transitions between letters and numbers
235
 
                //     (so "cvs2svn" becomes "cvs 2 svn")
 
239
                /// <summary>
 
240
                ///  Here we:
 
241
                /// (1) Replace non-alphanumeric characters with spaces
 
242
                /// (2) Inject whitespace between lowercase-to-uppercase
 
243
                ///     transitions (so "FooBar" becomes "Foo Bar")
 
244
                ///     and transitions between letters and numbers
 
245
                ///     (so "cvs2svn" becomes "cvs 2 svn")
 
246
                /// </summary>
 
247
                /// <param name="line">
 
248
                /// A <see cref="System.String"/>
 
249
                /// </param>
 
250
                /// <returns>
 
251
                /// A <see cref="System.String"/>
 
252
                /// </returns>
236
253
                static public string FuzzyDivide (string line)
237
254
                {
238
255
                        // Allocate a space slightly bigger than the
282
299
                        return FuzzyDivide (url.Substring (protocol_index + 3));
283
300
                }
284
301
                
285
 
                // Match strings against patterns that are allowed to contain
286
 
                // glob-style * and ? wildcards.
 
302
                /// <summary>
 
303
                ///      Match strings against patterns that are allowed to contain
 
304
                ///      glob-style * and ? wildcards.
 
305
                /// </summary>
 
306
                /// <param name="pattern">
 
307
                /// A <see cref="System.String"/>
 
308
                /// </param>
 
309
                /// <param name="str">
 
310
                /// A <see cref="System.String"/>
 
311
                /// </param>
 
312
                /// <returns>
 
313
                /// A <see cref="System.Boolean"/>
 
314
                /// </returns>
287
315
                static public bool GlobMatch (string pattern, string str)
288
316
                {
289
317
                        if (pattern == null || str == null)
300
328
                
301
329
                private const char WILDCARD_STRING = '*';
302
330
                
303
 
                // Copied from beagled/Lucene.Net/Search/WildcardTermEnum.cs
304
 
                // Simple string matching algorithm with wildcards
305
 
                // '*' matches 0 or more characters
 
331
                /// <summary>
 
332
                /// Copied from beagled/Lucene.Net/Search/WildcardTermEnum.cs
 
333
                /// Simple string matching algorithm with wildcards
 
334
                /// '*' matches 0 or more characters
 
335
                /// </summary>
 
336
                /// <param name="pattern">
 
337
                /// A <see cref="System.String"/>
 
338
                /// </param>
 
339
                /// <param name="patternIdx">
 
340
                /// A <see cref="System.Int32"/>
 
341
                /// </param>
 
342
                /// <param name="text">
 
343
                /// A <see cref="System.String"/>
 
344
                /// </param>
 
345
                /// <param name="stringIdx">
 
346
                /// A <see cref="System.Int32"/>
 
347
                /// </param>
 
348
                /// <returns>
 
349
                /// A <see cref="System.Boolean"/>
 
350
                /// </returns>
306
351
                private static bool WildcardEquals(System.String pattern, int patternIdx, System.String text, int stringIdx)
307
352
                {
308
353
                        int p = patternIdx;
415
460
 
416
461
                static public string HexEscape (string str)
417
462
                {
418
 
                        StringBuilder builder = new StringBuilder ();
419
 
 
420
 
                        foreach (char c in str) {
 
463
                        int index = -1;
 
464
                        if ((index = str.IndexOfAny (CharsToQuote)) == -1)
 
465
                                return str;
 
466
 
 
467
                        StringBuilder builder = new StringBuilder (str, 0, index, str.Length << 1);
 
468
 
 
469
                        for (; index < str.Length; ++ index) {
 
470
                                char c = str [index];
421
471
 
422
472
                                if (ArrayFu.IndexOfChar (CharsToQuote, c) != -1)
423
473
                                        builder.Append (Uri.HexEscape (c));
436
486
                        return builder.ToString ();
437
487
                }
438
488
 
439
 
                // Translate all %xx codes into real characters
 
489
                /// <summary>
 
490
                /// Translate all %xx codes into real characters
 
491
                /// </summary>
 
492
                /// <param name="str">
 
493
                /// A <see cref="System.String"/>
 
494
                /// </param>
 
495
                /// <returns>
 
496
                /// A <see cref="System.String"/>
 
497
                /// </returns>
440
498
                static public string HexUnescape (string str)
441
499
                {
442
 
                        ArrayList bytes = new ArrayList ();
 
500
                        int i, pos = 0;
 
501
                        if ((i = str.IndexOf ('%')) == -1)
 
502
                                return str;
 
503
 
 
504
                        List<byte> bytes = new List<byte> (str.Length);
443
505
                        byte[] sub_bytes;
444
 
                        int i, pos = 0;
445
506
 
446
 
                        while ((i = str.IndexOf ('%', pos)) != -1) {
 
507
                        do {
447
508
                                sub_bytes = Encoding.UTF8.GetBytes (str.Substring (pos, i - pos));
448
509
                                bytes.AddRange (sub_bytes);
449
510
                                
450
511
                                pos = i;
451
512
                                char unescaped = Uri.HexUnescape (str, ref pos);
452
 
                                bytes.Add ((byte) unescaped);
453
 
                        }
 
513
                                bytes.Add (Convert.ToByte (unescaped));
 
514
                        } while ((i = str.IndexOf ('%', pos)) != -1);
454
515
 
455
516
                        sub_bytes = Encoding.UTF8.GetBytes (str.Substring (pos, str.Length - pos));
456
517
                        bytes.AddRange (sub_bytes);
457
518
 
458
 
                        return Encoding.UTF8.GetString ((byte[]) bytes.ToArray (typeof (byte)));
 
519
                        return Encoding.UTF8.GetString (bytes.ToArray ());
459
520
                }
460
521
 
461
522
                // These strings should never be exposed to the user.
605
666
                        return new string (char_array);
606
667
                }
607
668
                
608
 
                // Words of less than min_word_length characters are not counted
 
669
                /// <summary>
 
670
                /// Words of less than min_word_length characters are not counted
 
671
                /// </summary>
 
672
                /// <param name="str">
 
673
                /// A <see cref="System.String"/>
 
674
                /// </param>
 
675
                /// <param name="max_words">
 
676
                /// A <see cref="System.Int32"/>
 
677
                /// </param>
 
678
                /// <param name="min_word_length">
 
679
                /// A <see cref="System.Int32"/>
 
680
                /// </param>
 
681
                /// <returns>
 
682
                /// A <see cref="System.Int32"/>
 
683
                /// </returns>
609
684
                static public int CountWords (string str, int max_words, int min_word_length)
610
685
                {
611
686
                        if (str == null)
645
720
                        return CountWords (str, -1);
646
721
                }
647
722
 
648
 
                // Strip trailing slashes and make sure we only have 1 leading slash
 
723
                /// <summary>
 
724
                /// Strip trailing slashes and make sure we only have 1 leading slash
 
725
                /// </summary>
 
726
                /// <param name="path">
 
727
                /// A <see cref="System.String"/>
 
728
                /// </param>
 
729
                /// <returns>
 
730
                /// A <see cref="System.String"/>
 
731
                /// </returns>
649
732
                static public string SanitizePath (string path)
650
733
                {
651
734
                        if (path.StartsWith ("//")) {
662
745
                        return path;
663
746
                }
664
747
 
665
 
                // This method will translate an email address like
666
 
                // "john.doe+spamtrap@foo.com" to "john doe spamtrap foo"
667
 
                //
668
 
                // FIXME: Maybe we should only do the username part?  Ie,
669
 
                // "john doe spamtrap"?  That way searching for "foo" won't
670
 
                // turn up *everything*
 
748
                /// <summary>
 
749
                /// This method will translate an email address like
 
750
                /// "john.doe+spamtrap@foo.com" to "john doe spamtrap foo"
 
751
                ///
 
752
                /// FIXME: Maybe we should only do the username part?  Ie,
 
753
                /// "john doe spamtrap"?  That way searching for "foo" won't
 
754
                /// turn up *everything*
 
755
                /// </summary>
 
756
                /// <param name="email">
 
757
                /// A <see cref="System.String"/>
 
758
                /// </param>
 
759
                /// <returns>
 
760
                /// A <see cref="System.String"/>
 
761
                /// </returns>
671
762
                static public string SanitizeEmail (string email)
672
763
                {
673
764
                        char[] replace_array = { '@', '.', '-', '_', '+' };
691
782
                        return email;
692
783
                }
693
784
 
694
 
                /**
695
 
                 * expands environment variables in a string e.g.
696
 
                 * folders=$HOME/.kde/share/...
697
 
                 */
 
785
                
 
786
                /// <summary>
 
787
                /// expands environment variables in a string e.g.
 
788
                /// folders=$HOME/.kde/share/...
 
789
                /// </summary>
 
790
                /// <param name="path">
 
791
                /// A <see cref="System.String"/>
 
792
                /// </param>
 
793
                /// <returns>
 
794
                /// A <see cref="System.String"/>
 
795
                /// </returns>
698
796
                public static string ExpandEnvVariables (string path)
699
797
                {
700
798
                        int dollar_pos = path.IndexOf ('$');