Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
StringExtensions.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 
7 namespace SiliconStudio.Presentation.Extensions
8 {
9  public static class StringExtensions
10  {
11  public const int MaxStackTraceLines = 30;
12 
13  public static List<string> CamelCaseSplit(this string str)
14  {
15  var result = new List<string>();
16  int wordStart = 0;
17  int wordLength = 0;
18  char prevChar = '\0';
19 
20  foreach (char currentChar in str)
21  {
22  if (prevChar != '\0')
23  {
24  // aA -> split between a and A
25  if (char.IsLower(prevChar) && char.IsUpper(currentChar))
26  {
27  var word = str.Substring(wordStart, wordLength);
28  result.Add(word);
29  wordStart += wordLength;
30  wordLength = 0;
31  }
32  // This will manage abbreviation words that does not contain lower case character: ABCDef should split into ABC and Def
33  if (char.IsUpper(prevChar) && char.IsLower(currentChar) && wordLength > 1)
34  {
35  var word = str.Substring(wordStart, wordLength - 1);
36  result.Add(word);
37  wordStart += wordLength - 1;
38  wordLength = 1;
39  }
40  }
41  ++wordLength;
42  prevChar = currentChar;
43  }
44 
45  result.Add(str.Substring(wordStart, wordLength));
46 
47  return result;
48  }
49 
50  public static string FormatExceptionForMessageBox(Exception exception, bool startWithNewLine)
51  {
52  // Get the innermost exception.
53  while (exception.InnerException != null)
54  {
55  exception = exception.InnerException;
56  }
57  var stackTrace = ExtractStackTrace(exception, MaxStackTraceLines);
58  return string.Format("{0}{1}{2}{3}", startWithNewLine ? Environment.NewLine : "", exception.Message, Environment.NewLine, stackTrace);
59  }
60 
61  public static string FormatExceptionForReport(Exception exception)
62  {
63  var message = string.Format("{0}{1}{2}{3}", exception.Message, Environment.NewLine, ExtractStackTrace(exception), Environment.NewLine);
64  var aggregateException = exception as AggregateException;
65  if (aggregateException != null)
66  {
67  message = aggregateException.InnerExceptions.Aggregate(message, (current, innerException) => current + FormatExceptionForReport(exception));
68  }
69 
70  if (exception.InnerException != null)
71  {
72  message += FormatExceptionForReport(exception.InnerException);
73  }
74  return message;
75  }
76 
77  private static string ExtractStackTrace(Exception exception, int maxLines = -1)
78  {
79  var stackTraceArray = exception.StackTrace.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
80  return string.Join(Environment.NewLine, maxLines > 0 ? stackTraceArray.Take(maxLines) : stackTraceArray);
81  }
82  }
83 }
static string FormatExceptionForMessageBox(Exception exception, bool startWithNewLine)
static string FormatExceptionForReport(Exception exception)
static List< string > CamelCaseSplit(this string str)