A class containing various string helper methods.
Example #1
0
        private static string DeriveCustomFormat(string strDT, DateTime dt)
        {
            string[] vPlh = new string[] {
                // Names, sorted by length
                "MMMM", "dddd",
                "MMM", "ddd",
                "gg", "g",

                // Numbers, the ones with prefix '0' first
                "yyyy", "yyy", "yy", "y",
                "MM", "M",
                "dd", "d",
                "HH", "hh", "H", "h",
                "mm", "m",
                "ss", "s",

                "tt", "t"
            };

            List <string> lValues = new List <string>();

            foreach (string strPlh in vPlh)
            {
                string strEval = strPlh;
                if (strEval.Length == 1)
                {
                    strEval = @"%" + strPlh;                                     // Make custom
                }
                lValues.Add(dt.ToString(strEval));
            }

            StringBuilder sbAll = new StringBuilder();

            sbAll.Append("dfFghHKmMstyz:/\"\'\\%");
            sbAll.Append(strDT);
            foreach (string strVEnum in lValues)
            {
                sbAll.Append(strVEnum);
            }

            List <char> lCodes = new List <char>();

            for (int i = 0; i < vPlh.Length; ++i)
            {
                char ch = StrUtil.GetUnusedChar(sbAll.ToString());
                lCodes.Add(ch);
                sbAll.Append(ch);
            }

            string str = strDT;

            for (int i = 0; i < vPlh.Length; ++i)
            {
                string strValue = lValues[i];
                if (string.IsNullOrEmpty(strValue))
                {
                    continue;
                }

                str = str.Replace(strValue, new string(lCodes[i], 1));
            }

            StringBuilder sbFmt      = new StringBuilder();
            bool          bInLiteral = false;

            foreach (char ch in str)
            {
                int iCode = lCodes.IndexOf(ch);

                // The escape character doesn't work correctly (e.g.
                // "dd\\.MM\\.yyyy\\ HH\\:mm\\:ss" doesn't work, but
                // "dd'.'MM'.'yyyy' 'HH':'mm':'ss" does); use '' instead

                // if(iCode >= 0) sbFmt.Append(vPlh[iCode]);
                // else // Literal
                // {
                //	sbFmt.Append('\\');
                //	sbFmt.Append(ch);
                // }

                if (iCode >= 0)
                {
                    if (bInLiteral)
                    {
                        sbFmt.Append('\''); bInLiteral = false;
                    }
                    sbFmt.Append(vPlh[iCode]);
                }
                else                 // Literal
                {
                    if (!bInLiteral)
                    {
                        sbFmt.Append('\''); bInLiteral = true;
                    }
                    sbFmt.Append(ch);
                }
            }
            if (bInLiteral)
            {
                sbFmt.Append('\'');
            }

            return(sbFmt.ToString());
        }
Example #2
0
        private static string ObjectsToMessage(object[] vLines, bool bFullExceptions)
        {
            if (vLines == null)
            {
                return(string.Empty);
            }

            var strNewPara = MessageService.NewParagraph;

            var sbText     = new StringBuilder();
            var bSeparator = false;

            foreach (var obj in vLines)
            {
                if (obj == null)
                {
                    continue;
                }

                string strAppend = null;

                var exObj  = (obj as Exception);
                var strObj = (obj as string);
#if !KeePassLibSD
                var scObj = (obj as StringCollection);
#endif

                if (exObj != null)
                {
                    if (bFullExceptions)
                    {
                        strAppend = StrUtil.FormatException(exObj);
                    }
                    else if (!string.IsNullOrEmpty(exObj.Message))
                    {
                        strAppend = exObj.Message;
                    }
                }
#if !KeePassLibSD
                else if (scObj != null)
                {
                    var sb = new StringBuilder();
                    foreach (var strCollLine in scObj)
                    {
                        if (sb.Length > 0)
                        {
                            sb.AppendLine();
                        }
                        sb.Append(strCollLine.TrimEnd());
                    }
                    strAppend = sb.ToString();
                }
#endif
                else if (strObj != null)
                {
                    strAppend = strObj;
                }
                else
                {
                    strAppend = obj.ToString();
                }

                if (!string.IsNullOrEmpty(strAppend))
                {
                    if (bSeparator)
                    {
                        sbText.Append(strNewPara);
                    }
                    else
                    {
                        bSeparator = true;
                    }

                    sbText.Append(strAppend);
                }
            }

            return(sbText.ToString());
        }