/// <summary> /// Cleans a string value to make it viable for the Swiss Payment Standards 2018. /// <para> /// Unsupported characters(according to Swiss Payment Standards 2018, ch. 2.4.1 /// and appendix D) are replaced with spaces(unsupported whitespace) or dots /// (all other unsupported characters). Leading and trailing whitespace is /// removed. /// </para> /// <para> /// If characters beyond 0xff are detected, the string is first normalized such /// that letters with umlauts or accents expressed with two code points are /// merged into a single code point(if possible), some of which might become /// valid. /// </para> /// <para> /// If the resulting strings is all white space, <c>null</c> is returned. /// </para> /// </summary> /// <param name="value">The string value to clean.</param> /// <param name="result">The result to be filled with cleaned string and flag.</param> public static void CleanValue(string value, out CleaningResult result) { CleanValue(value, out result, false); if (result.CleanedString != null && result.CleanedString.Length == 0) { result.CleanedString = null; } }
private CleaningResult StoreResult(int numCommands, long uniquePlaces, double runtime) { var result = new CleaningResult { commands = numCommands, result = uniquePlaces, timestamp = DateTime.Now, duration = runtime }; _context.CleaningResult.Add(result); _context.SaveChanges(); return(result); }
public static bool VycistiZnackovaniDokumentu(string souborDocx, bool zalohovat, string novySoubor) { string dokument = GetTempFileName(); ZalohovatDokument(souborDocx, zalohovat); ExtrahovatDoSouboru(souborDocx, dokument, true); DocxCleaner cleaner = new DocxCleaner(); CleaningResult result = cleaner.Clean(dokument); if (result.Success) { string zaloha = GetTempFileName(); if (souborDocx != novySoubor) { File.Delete(zaloha); File.Copy(souborDocx, zaloha); } NahraditDokument(souborDocx, result.Output, zalohovat); if (souborDocx != novySoubor) { if (File.Exists(novySoubor)) { File.Delete(novySoubor); } File.Move(souborDocx, novySoubor); File.Move(zaloha, souborDocx); } } return(result.Success); }
#pragma warning disable S3776 private static void CleanValue(string value, out CleaningResult result, bool isNormalized) { result = new CleaningResult(); if (value == null) { return; } int len = value.Length; // length of value bool justProcessedSpace = false; // flag indicating whether we've just processed a space character StringBuilder sb = null; // String builder for result int lastCopiedPos = 0; // last position (excluding) copied to the result // String processing pattern: Iterate all characters and focus on runs of valid // characters that can simply be copied. If all characters are valid, no memory // is allocated. int pos = 0; while (pos < len) { char ch = value[pos]; // current character if (IsValidQrBillCharacter(ch)) { justProcessedSpace = ch == ' '; pos++; continue; } // Check for normalization if (ch > 0xff && !isNormalized) { isNormalized = value.IsNormalized(NormalizationForm.FormC); if (!isNormalized) { // Normalize string and start over value = value.Normalize(NormalizationForm.FormC); CleanValue(value, out result, true); return; } } if (sb == null) { sb = new StringBuilder(value.Length); } // copy processed characters to result before taking care of the invalid // character if (pos > lastCopiedPos) { sb.Append(value, lastCopiedPos, pos - lastCopiedPos); } if (char.IsHighSurrogate(ch)) { // Proper Unicode handling to prevent surrogates and combining characters // from being replaced with multiples periods. UnicodeCategory category = CharUnicodeInfo.GetUnicodeCategory(value, pos); if (category != UnicodeCategory.SpacingCombiningMark) { sb.Append('.'); } justProcessedSpace = false; pos++; } else { if (ch <= ' ') { if (!justProcessedSpace) { sb.Append(' '); } justProcessedSpace = true; } else { sb.Append('.'); justProcessedSpace = false; } } pos++; lastCopiedPos = pos; } if (sb == null) { result.CleanedString = value.Trim(); return; } if (lastCopiedPos < len) { sb.Append(value, lastCopiedPos, len - lastCopiedPos); } result.CleanedString = sb.ToString().Trim(); result.ReplacedUnsupportedChars = true; }