forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringHelper.cs
875 lines (780 loc) · 23.9 KB
/
StringHelper.cs
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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NHibernate.Util
{
/// <summary></summary>
public static class StringHelper
{
/// <summary>
/// This allows for both CRLF and lone LF line separators.
/// </summary>
internal static readonly string[] LineSeparators = {"\r\n", "\n"};
public const string WhiteSpace = " \n\r\f\t";
/// <summary></summary>
public const char Dot = '.';
/// <summary></summary>
public const char Underscore = '_';
/// <summary></summary>
public const string CommaSpace = ", ";
/// <summary></summary>
public const string Comma = ",";
/// <summary></summary>
public const string OpenParen = "(";
/// <summary></summary>
public const string ClosedParen = ")";
/// <summary></summary>
public const char SingleQuote = '\'';
/// <summary></summary>
public const string SqlParameter = "?";
public const int AliasTruncateLength = 10;
//Since 5.3
[Obsolete("Please use string.Join instead")]
public static string Join(string separator, IEnumerable objects)
{
StringBuilder buf = new StringBuilder();
bool first = true;
foreach (object obj in objects)
{
if (!first)
{
buf.Append(separator);
}
first = false;
buf.Append(obj);
}
return buf.ToString();
}
/// <summary>
///
/// </summary>
/// <param name="str"></param>
/// <param name="times"></param>
/// <returns></returns>
public static string Repeat(string str, int times)
{
StringBuilder buf = new StringBuilder(str.Length * times);
for (int i = 0; i < times; i++)
{
buf.Append(str);
}
return buf.ToString();
}
//Since v5.3
[Obsolete("Please use string.Replace or Regex.Replace instead.")]
public static string Replace(string template, string placeholder, string replacement)
{
// sometimes a null value will get passed in here -> SqlWhereStrings are a good example
return template?.Replace(placeholder, replacement);
}
//Since v5.3
[Obsolete("Please use string.Replace or Regex.Replace instead.")]
public static string Replace(string template, string placeholder, string replacement, bool wholeWords)
{
Predicate<string> isWholeWord = c => WhiteSpace.Contains(c) || ClosedParen.Equals(c) || Comma.Equals(c);
return ReplaceByPredicate(template, placeholder, replacement, wholeWords, isWholeWord);
}
private static string ReplaceByPredicate(string template, string placeholder, string replacement, bool useWholeWord, Predicate<string> isWholeWord)
{
// sometimes a null value will get passed in here -> SqlWhereStrings are a good example
if (string.IsNullOrWhiteSpace(template))
{
return null;
}
int loc = template.IndexOf(placeholder);
if (loc < 0)
{
return template;
}
else
{
// NH different implementation (NH-1253)
string replaceWith = replacement;
if (loc + placeholder.Length < template.Length)
{
string afterPlaceholder = template[loc + placeholder.Length].ToString();
//After a token in HQL there can be whitespace, closedparen or comma..
if (useWholeWord && !isWholeWord(afterPlaceholder))
{
//If this is not a full token we don't want to touch it
replaceWith = placeholder;
}
}
return new StringBuilder(template.Substring(0, loc))
.Append(replaceWith)
.Append(ReplaceByPredicate(template.Substring(loc + placeholder.Length), placeholder, replacement, useWholeWord, isWholeWord))
.ToString();
}
}
//Since v5.3
[Obsolete("Please use string.Replace or Regex.Replace instead.")]
public static string ReplaceWholeWord(this string template, string placeholder, string replacement)
{
Predicate<string> isWholeWord = s => !Char.IsLetterOrDigit(s[0]);
return ReplaceByPredicate(template, placeholder, replacement, true, isWholeWord);
}
/// <summary>
///
/// </summary>
/// <param name="template"></param>
/// <param name="placeholder"></param>
/// <param name="replacement"></param>
/// <returns></returns>
public static string ReplaceOnce(string template, string placeholder, string replacement)
{
int loc = template.IndexOf(placeholder);
if (loc < 0)
{
return template;
}
else
{
return new StringBuilder(template.Substring(0, loc))
.Append(replacement)
.Append(template.Substring(loc + placeholder.Length))
.ToString();
}
}
/// <summary>
/// Just a facade for calling string.Split()
/// We don't use our StringTokenizer because string.Split() is
/// more efficient (but it only works when we don't want to retrieve the delimiters)
/// </summary>
/// <param name="separators">separators for the tokens of the list</param>
/// <param name="list">the string that will be broken into tokens</param>
/// <returns></returns>
public static string[] Split(string separators, string list)
{
return list.Split(separators.ToCharArray());
}
/// <summary>
/// Splits the String using the StringTokenizer.
/// </summary>
/// <param name="separators">separators for the tokens of the list</param>
/// <param name="list">the string that will be broken into tokens</param>
/// <param name="include">true to include the separators in the tokens.</param>
/// <returns></returns>
/// <remarks>
/// This is more powerful than Split because you have the option of including or
/// not including the separators in the tokens.
/// </remarks>
public static string[] Split(string separators, string list, bool include)
{
var tokens = new StringTokenizer(list, separators, include);
return tokens.ToArray();
}
/// <summary>
///
/// </summary>
/// <param name="qualifiedName"></param>
/// <returns></returns>
public static string Unqualify(string qualifiedName)
{
if (qualifiedName.IndexOf('`') > 0)
{
// less performance but correctly manage generics classes
// where the entity-name was not specified
// Note: the enitty-name is mandatory when the user want work with different type-args
// for the same generic-entity implementation
return GetClassname(qualifiedName);
}
return Unqualify(qualifiedName, '.');
}
/// <summary>
///
/// </summary>
/// <param name="qualifiedName"></param>
/// <param name="seperator"></param>
/// <returns></returns>
public static string Unqualify(string qualifiedName, string seperator)
{
return qualifiedName.Substring(qualifiedName.LastIndexOf(seperator) + 1);
}
internal static string Unqualify(string qualifiedName, char seperator)
{
return qualifiedName.Substring(qualifiedName.LastIndexOf(seperator) + 1);
}
/// <summary>
/// Takes a fully qualified type name and returns the full name of the
/// Class - includes namespaces.
/// </summary>
/// <param name="typeName"></param>
/// <returns></returns>
public static string GetFullClassname(string typeName)
{
return new TypeNameParser(null, null).ParseTypeName(typeName).Type;
}
/// <summary>
/// Takes a fully qualified type name (can include the assembly) and just returns
/// the name of the Class.
/// </summary>
/// <param name="typeName"></param>
/// <returns></returns>
public static string GetClassname(string typeName)
{
//string[] splitClassname = GetFullClassname(typeName).Split('.');
string fullClassName = GetFullClassname(typeName);
int genericTick = fullClassName.IndexOf('`');
if (genericTick != -1)
{
string nameBeforeGenericTick = fullClassName.Substring(0, genericTick);
int lastPeriod = nameBeforeGenericTick.LastIndexOf('.');
return lastPeriod != -1 ? fullClassName.Substring(lastPeriod + 1) : fullClassName;
}
string[] splitClassname = fullClassName.Split('.');
return splitClassname[splitClassname.Length - 1];
}
/// <summary>
///
/// </summary>
/// <param name="qualifiedName"></param>
/// <returns></returns>
public static string Qualifier(string qualifiedName)
{
int loc = qualifiedName.LastIndexOf('.');
if (loc < 0)
{
return String.Empty;
}
else
{
return qualifiedName.Substring(0, loc);
}
}
/// <summary>
///
/// </summary>
/// <param name="columns"></param>
/// <param name="suffix"></param>
/// <returns></returns>
public static string[] Suffix(string[] columns, string suffix)
{
if (suffix == null)
{
return columns;
}
string[] qualified = new string[columns.Length];
for (int i = 0; i < columns.Length; i++)
{
qualified[i] = Suffix(columns[i], suffix);
}
return qualified;
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <param name="suffix"></param>
/// <returns></returns>
public static string Suffix(string name, string suffix)
{
return (suffix == null) ?
name :
name + suffix;
}
/// <summary>
///
/// </summary>
/// <param name="columns"></param>
/// <param name="prefix"></param>
/// <returns></returns>
public static string[] Prefix(string[] columns, string prefix)
{
if (prefix == null)
{
return columns;
}
string[] qualified = new string[columns.Length];
for (int i = 0; i < columns.Length; i++)
{
qualified[i] = prefix + columns[i];
}
return qualified;
}
/// <summary>
///
/// </summary>
/// <param name="qualifiedName"></param>
/// <returns></returns>
public static string Root(string qualifiedName)
{
int loc = qualifiedName.IndexOf('.');
return (loc < 0)
? qualifiedName
: qualifiedName.Substring(0, loc);
}
/// <summary>
/// Returns true if given name is not root property name
/// </summary>
/// <param name="qualifiedName"></param>
/// <param name="root">Returns root name</param>
internal static bool IsNotRoot(string qualifiedName, out string root)
{
root = qualifiedName;
int loc = qualifiedName.IndexOf('.');
if (loc < 0)
return false;
root = qualifiedName.Substring(0, loc);
return true;
}
/// <summary>
/// Returns true if given name is not root property name
/// </summary>
/// <param name="qualifiedName"></param>
/// <param name="root">Returns root name</param>
/// <param name="unrootPath">Returns "unrooted" name, or empty string for root </param>
/// <returns></returns>
internal static bool IsNotRoot(string qualifiedName, out string root, out string unrootPath)
{
unrootPath = string.Empty;
root = qualifiedName;
int loc = qualifiedName.IndexOf('.');
if (loc < 0)
return false;
unrootPath = qualifiedName.Substring(loc + 1);
root = qualifiedName.Substring(0, loc);
return true;
}
/// <summary>
/// Returns true if supplied fullPath has non empty pathToProperty
/// "alias.Entity.Value" -> pathToProperty = "alias.Entity", propertyName = "Value"
/// </summary>
internal static bool ParsePathAndPropertyName(string fullPath, out string pathToProperty, out string propertyName)
{
propertyName = fullPath;
pathToProperty = string.Empty;
int loc = fullPath.LastIndexOf('.');
if (loc < 0)
return false;
propertyName = fullPath.Substring(loc + 1);
pathToProperty = fullPath.Substring(0, loc);
return true;
}
/// <summary>
/// Converts a <see cref="String"/> in the format of "true", "t", "false", or "f" to
/// a <see cref="Boolean"/>.
/// </summary>
/// <param name="value">The string to convert.</param>
/// <returns>
/// The <c>value</c> converted to a <see cref="Boolean"/> .
/// </returns>
public static bool BooleanValue(string value)
{
string trimmed = value.Trim();
return trimmed.Equals("true", StringComparison.OrdinalIgnoreCase) || trimmed.Equals("t", StringComparison.OrdinalIgnoreCase);
}
private static string NullSafeToString(object obj)
{
return obj == null ? "(null)" : obj.ToString();
}
/// <summary>
///
/// </summary>
/// <param name="array"></param>
/// <returns></returns>
public static string ToString(object[] array)
{
int len = array.Length;
// if there is no value in the array then return no string...
if (len == 0)
{
return String.Empty;
}
StringBuilder buf = new StringBuilder(len * 12);
for (int i = 0; i < len - 1; i++)
{
buf.Append(NullSafeToString(array[i])).Append(CommaSpace);
}
return buf.Append(NullSafeToString(array[len - 1])).ToString();
}
public static string LinesToString(this string[] text)
{
if (text == null)
{
return null;
}
if (text.Length == 1)
{
return text[0];
}
var sb = new StringBuilder(200);
Array.ForEach(text, t => sb.AppendLine(t));
return sb.ToString();
}
/// <summary>
///
/// </summary>
/// <param name="str"></param>
/// <param name="placeholders"></param>
/// <param name="replacements"></param>
/// <returns></returns>
public static string[] Multiply(string str, IEnumerable<object> placeholders, IEnumerable<object> replacements)
{
var result = new [] { str };
using (var replacementsIterator = replacements.GetEnumerator())
{
foreach (var placeholder in placeholders)
{
replacementsIterator.MoveNext();
result = Multiply(result, placeholder as string, replacementsIterator.Current as string[]);
}
}
return result;
}
/// <summary>
///
/// </summary>
/// <param name="strings"></param>
/// <param name="placeholder"></param>
/// <param name="replacements"></param>
/// <returns></returns>
public static string[] Multiply(string[] strings, string placeholder, string[] replacements)
{
string[] results = new string[replacements.Length * strings.Length];
int n = 0;
for (int i = 0; i < replacements.Length; i++)
{
for (int j = 0; j < strings.Length; j++)
{
results[n++] = ReplaceOnce(strings[j], placeholder, replacements[i]);
}
}
return results;
}
/// <summary>
/// Counts the unquoted instances of the character.
/// </summary>
/// <param name="str"></param>
/// <param name="character"></param>
/// <returns></returns>
public static int CountUnquoted(string str, char character)
{
if (SingleQuote == character)
{
throw new ArgumentOutOfRangeException("character", "Unquoted count of quotes is invalid");
}
// Impl note: takes advantage of the fact that an escaped single quote
// embedded within a quote-block can really be handled as two separate
// quote-blocks for the purposes of this method...
int count = 0;
char[] chars = str.ToCharArray();
int stringLength = string.IsNullOrEmpty(str) ? 0 : chars.Length;
bool inQuote = false;
for (int indx = 0; indx < stringLength; indx++)
{
if (inQuote)
{
if (SingleQuote == chars[indx])
{
inQuote = false;
}
}
else if (SingleQuote == chars[indx])
{
inQuote = true;
}
else if (chars[indx] == character)
{
count++;
}
}
return count;
}
public static bool IsEmpty(string str)
{
return string.IsNullOrEmpty(str);
}
public static bool IsNotEmpty(string str)
{
return !IsEmpty(str);
}
/// <summary>
///
/// </summary>
/// <param name="prefix"></param>
/// <param name="name"></param>
/// <returns></returns>
public static string Qualify(string prefix, string name)
{
char first = name[0];
// Should we check for prefix == string.Empty rather than a length check?
if (!string.IsNullOrEmpty(prefix) && first != SingleQuote && !char.IsDigit(first))
{
return prefix + Dot + name;
}
else
{
return name;
}
}
public static string[] Qualify(string prefix, string[] names)
{
// Should we check for prefix == string.Empty rather than a length check?
if (!string.IsNullOrEmpty(prefix))
{
int len = names.Length;
string[] qualified = new string[len];
for (int i = 0; i < len; i++)
{
qualified[i] = names[i] == null ? null : Qualify(prefix, names[i]);
}
return qualified;
}
else
{
return names;
}
}
public static int FirstIndexOfChar(string sqlString, string str, int startIndex)
{
return FirstIndexOfChar(sqlString, str.ToCharArray(), startIndex);
}
internal static int FirstIndexOfChar(string sqlString, char[] chars, int startIndex)
{
return sqlString.IndexOfAny(chars, startIndex);
}
public static string Truncate(string str, int length)
{
if (str.Length <= length)
{
return str;
}
else
{
return str.Substring(0, length);
}
}
public static int LastIndexOfLetter(string str)
{
for (int i = 0; i < str.Length; i++)
{
if (!char.IsLetter(str, i) /*&& !('_'==character)*/)
{
return i - 1;
}
}
return str.Length - 1;
}
public static string UnqualifyEntityName(string entityName)
{
string result = Unqualify(entityName);
int slashPos = result.IndexOf('/');
if (slashPos > 0)
{
result = result.Substring(0, slashPos - 1);
}
return result;
}
public static string GenerateAlias(string description)
{
return GenerateAliasRoot(description) + Underscore;
}
/// <summary>
/// Generate a nice alias for the given class name or collection role
/// name and unique integer. Subclasses do <em>not</em> have to use
/// aliases of this form.
/// </summary>
/// <returns>an alias of the form <c>foo1_</c></returns>
public static string GenerateAlias(string description, int unique)
{
return GenerateAliasRoot(description) +
unique +
Underscore;
}
private static string GenerateAliasRoot(string description)
{
// Remove any generic arguments attached to description
int indexOfBacktick = description.IndexOf('`');
if (indexOfBacktick > 0)
{
description = Truncate(description, indexOfBacktick);
}
string result = Truncate(UnqualifyEntityName(description), AliasTruncateLength)
.ToLowerInvariant()
.Replace('/', '_') // entityNames may now include slashes for the representations
.Replace('+', '_') // classname may be an inner class
.Replace('[', '_') // classname may contain brackets
.Replace(']', '_')
.Replace('`', '_') // classname may contain backticks (generic types)
.TrimStart('_') // Remove underscores from the beginning of the alias (for Firebird).
;
if (char.IsDigit(result, result.Length - 1))
{
return result + "x"; //ick!
}
if (char.IsLetter(result[0]) || '_' == result[0])
{
return result;
}
return "alias_" + result;
}
public static string MoveAndToBeginning(string filter)
{
if (!string.IsNullOrWhiteSpace(filter))
{
filter += " and ";
if (filter.StartsWith(" and ", StringComparison.Ordinal))
{
filter = filter.Substring(4);
}
}
return filter;
}
public static string Unroot(string qualifiedName)
{
int loc = qualifiedName.IndexOf('.');
return (loc < 0) ? qualifiedName : qualifiedName.Substring(loc + 1);
}
// Since 5.2
[Obsolete("This method has no more usage and will be removed in a future version")]
public static bool EqualsCaseInsensitive(string a, string b)
{
return StringComparer.InvariantCultureIgnoreCase.Compare(a, b) == 0;
}
// Since 5.2
[Obsolete("This method has no more usage and will be removed in a future version")]
public static int IndexOfCaseInsensitive(string source, string value)
{
return source.IndexOf(value, StringComparison.InvariantCultureIgnoreCase);
}
// Since 5.2
[Obsolete("This method has no more usage and will be removed in a future version")]
public static int IndexOfCaseInsensitive(string source, string value, int startIndex)
{
return source.IndexOf(value, startIndex, StringComparison.InvariantCultureIgnoreCase);
}
// Since 5.2
[Obsolete("This method has no more usage and will be removed in a future version")]
public static int IndexOfCaseInsensitive(string source, string value, int startIndex, int count)
{
return source.IndexOf(value, startIndex, count, StringComparison.InvariantCultureIgnoreCase);
}
// Since 5.2
[Obsolete("This method has no more usage and will be removed in a future version")]
public static int LastIndexOfCaseInsensitive(string source, string value)
{
return source.LastIndexOf(value, StringComparison.InvariantCultureIgnoreCase);
}
// Since 5.2
[Obsolete("This method has no more usage and will be removed in a future version")]
public static bool StartsWithCaseInsensitive(string source, string prefix)
{
return source.StartsWith(prefix, StringComparison.InvariantCultureIgnoreCase);
}
internal static bool ContainsCaseInsensitive(string source, string value)
{
return source.IndexOf(value, StringComparison.OrdinalIgnoreCase) >= 0;
}
internal static bool StartsWith(this string source, char value)
{
return source.Length > 0 && source[0] == value;
}
internal static bool EndsWith(this string source, char value)
{
return source.Length > 0 && source[source.Length - 1] == value;
}
/// <summary>
/// Returns the interned string equal to <paramref name="str"/> if there is one, or <paramref name="str"/>
/// otherwise.
/// </summary>
/// <param name="str">A <see cref="string" /></param>
/// <returns>A <see cref="string" /></returns>
public static string InternedIfPossible(string str)
{
if (str == null)
{
return null;
}
string interned = string.IsInterned(str);
if (interned != null)
{
return interned;
}
return str;
}
public static string CollectionToString(IEnumerable keys)
{
var sb = new StringBuilder();
foreach (object o in keys)
{
sb.Append(o);
sb.Append(", ");
}
if (sb.Length != 0)//remove last ", "
sb.Remove(sb.Length - 2, 2);
return sb.ToString();
}
public static string ToUpperCase(string str)
{
return str == null ? null : str.ToUpperInvariant();
}
public static string ToLowerCase(string str)
{
return str == null ? null : str.ToLowerInvariant();
}
public static bool IsBackticksEnclosed(string identifier)
{
return !string.IsNullOrEmpty(identifier) && identifier.StartsWith('`') && identifier.EndsWith('`');
}
public static string PurgeBackticksEnclosing(string identifier)
{
if (IsBackticksEnclosed(identifier))
{
return identifier.Substring(1, identifier.Length - 2);
}
return identifier;
}
public static string[] ParseFilterParameterName(string filterParameterName)
{
int dot = filterParameterName.IndexOf('.');
if (dot <= 0)
{
throw new ArgumentException("Invalid filter-parameter name format; the name should be a property path.", "filterParameterName");
}
string filterName = filterParameterName.Substring(0, dot);
string parameterName = filterParameterName.Substring(dot + 1);
return new[] { filterName, parameterName };
}
/// <summary>
/// Return the index of the next line separator, starting at startIndex. If will match
/// the first CRLF or LF line separator. If there is no match, -1 will be returned. When
/// returning, newLineLength will be set to the number of characters in the matched line
/// separator (1 if LF was found, 2 if CRLF was found).
/// </summary>
public static int IndexOfAnyNewLine(this string str, int startIndex, out int newLineLength)
{
newLineLength = 0;
var matchStartIdx = str.IndexOfAny(new[] {'\r', '\n'}, startIndex);
if (matchStartIdx == -1)
return -1;
if (string.Compare(str, matchStartIdx, "\r\n", 0, 2, StringComparison.OrdinalIgnoreCase) == 0)
newLineLength = 2;
else
newLineLength = 1;
return matchStartIdx;
}
/// <summary>
/// Check if the given index points to a line separator in the string. Both CRLF and LF
/// line separators are handled. When returning, newLineLength will be set to the number
/// of characters matched in the line separator. It will be 2 if a CRLF matched, 1 if LF
/// matched, and 0 if the index doesn't indicate (the start of) a line separator.
/// </summary>
public static bool IsAnyNewLine(this string str, int index, out int newLineLength)
{
if (string.Compare(str, index, "\r\n", 0, 2, StringComparison.OrdinalIgnoreCase) == 0)
{
newLineLength = 2;
return true;
}
if (index < str.Length && str[index] == '\n')
{
newLineLength = 1;
return true;
}
newLineLength = 0;
return false;
}
}
}