Ejemplo n.º 1
0
        private static void _AppendCSVValue(StringBuilder sb, string appendValue, SimpleLatch notFirstItem)
        {
            //Normalize the input
            if (appendValue == null)
            {
                appendValue = "";
            }

            //Add a preceeding comma if we are not the first item
            if (notFirstItem.Value)
            {
                sb.Append(",");
            }
            var escapedValue = appendValue;

            var needsQuoting = new SimpleLatch();

            escapedValue = _ReplaceIfExists(escapedValue, "\"", "\"\"", needsQuoting); //Replace any single " with ""  (CSV convention)

            //escapedValue = escapedValue.Replace("\"", "\"\""); //Replace any single " with ""  (CSV convention)
            escapedValue = escapedValue.Replace("\n", " "); //Remove newlines
            escapedValue = escapedValue.Replace("\r", " "); //Remove carrage returns

            //If it has a comma it needs to be quoted
            if (escapedValue.Contains(","))
            {
                needsQuoting.Trigger();
            }
            //bool containsComma = escapedValue.Contains(",");

            if (needsQuoting.Value)
            {
                sb.Append("\"");
            }                                            //Start quote
            sb.Append(escapedValue);
            if (needsQuoting.Value)
            {
                sb.Append("\"");
            }                       //End quote
            notFirstItem.Trigger(); //No longer the first item
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Replace any 'find' with 'replace'.  Trigger the latch if a replace occured
        /// </summary>
        /// <param name="text"></param>
        /// <param name="find"></param>
        /// <param name="replace"></param>
        /// <param name="triggerIfFound"></param>
        /// <returns></returns>
        private static string _ReplaceIfExists(string text, string find, string replace, SimpleLatch triggerIfFound)
        {
            if (text == null)
            {
                return("");
            }
            //
            if (text.IndexOf(find, StringComparison.Ordinal) == -1)
            {
                return(text);
            }

            //If there's text to replace, then replace it
            var outText = text.Replace(find, replace);

            //If the text changed, trigger the latch
            if (outText != text)
            {
                //Set the latch
                triggerIfFound.Trigger();
            }

            return(outText);
        }