F2021-093: BNPP1 & BNPPalr - replace space with hard space between number & units

F2021-093: replace space with hard space between number & units
This commit is contained in:
Kathy Ruffing 2021-11-23 14:07:17 +00:00
parent 4f3bf70ebd
commit 799af11664
3 changed files with 113 additions and 59 deletions

Binary file not shown.

Binary file not shown.

View File

@ -2061,58 +2061,57 @@ namespace VEPROMS.CSLA.Library
// F2021-053: BNPP Alarm - need ability to have super/sub scripts in the text of Alarm Tables (ROs). // F2021-053: BNPP Alarm - need ability to have super/sub scripts in the text of Alarm Tables (ROs).
// if doing replace words for Page List items, the current item is not a step, use _DoReplWordInPageList flags this // if doing replace words for Page List items, the current item is not a step, use _DoReplWordInPageList flags this
if (_MyItemInfo.MyContent.Type < 20000 && !_DoReplWordInPageList) return Text; // for now only replace in steps. if (_MyItemInfo.MyContent.Type < 20000 && !_DoReplWordInPageList) return Text; // for now only replace in steps.
FormatConfig.ReplaceStrData rsl = _MyFormat.PlantFormat.UCFandOrigReplaceStrData;
if (rsl.Count == 1 && (rsl[0].ReplaceWord == null || rsl[0].ReplaceWord == "")) return Text;
// F2021-093: Handle partials first and then plain replace words. Need to do this so that the words don't get processed by plain replace
// words then not found when hitting the partials, for example cm2 (plain replaced to superscript 2) and then Kg/cm2 for partials is
// not found. Note that when this was done (11/2021) only BGE & BNPP had partials and BGE's were very limited.
Text = DoReplacePartials(Text, rsl);
VE_Font vf = _MyItemInfo.MyContent.Type >= 20000 ? _MyItemInfo.FormatStepData.Font : _MyItemInfo.ActiveFormat.PlantFormat.FormatData.Font; VE_Font vf = _MyItemInfo.MyContent.Type >= 20000 ? _MyItemInfo.FormatStepData.Font : _MyItemInfo.ActiveFormat.PlantFormat.FormatData.Font;
FoundMatches myMatches = new FoundMatches(Text,vf,_MyItemInfo); FoundMatches myMatches = new FoundMatches(Text,vf,_MyItemInfo);
// Exclude Link Text from Replace Word process // Exclude Link Text from Replace Word process
myMatches.AddLink(regFindLink, _MyFormat.PlantFormat.FormatData.SectData.ReplaceWordsInROs, _MyItemInfo.MyProcedure.MyDocVersion); myMatches.AddLink(regFindLink, _MyFormat.PlantFormat.FormatData.SectData.ReplaceWordsInROs, _MyItemInfo.MyProcedure.MyDocVersion);
FormatConfig.ReplaceStrData rsl = _MyFormat.PlantFormat.UCFandOrigReplaceStrData;
// ReplaceStrData xml node is empty, it does the inheritance and gets the 'base' format's list.
if (rsl.Count==1 && (rsl[0].ReplaceWord == null || rsl[0].ReplaceWord == "")) return Text;
// Loop through text looking for words to be replaced // Loop through text looking for words to be replaced
Dictionary<FormatConfig.ReplaceStr, Regex> partialReplaceList = new Dictionary<FormatConfig.ReplaceStr, Regex>();
Dictionary<E_ReplaceFlags?, bool> shouldReplace = new Dictionary<E_ReplaceFlags?, bool>(); Dictionary<E_ReplaceFlags?, bool> shouldReplace = new Dictionary<E_ReplaceFlags?, bool>();
//int profileDepth = ProfileTimer.Push(">>>> DoReplaceWords2.ForLoop"); //int profileDepth = ProfileTimer.Push(">>>> DoReplaceWords2.ForLoop");
foreach (FormatConfig.ReplaceStr rs in rsl) foreach (FormatConfig.ReplaceStr rs in rsl)
{ {
bool dopartial = (E_ReplaceFlags)(rs.Flag & FormatConfig.E_ReplaceFlagsUCF.Partials) == E_ReplaceFlags.Partials; bool dopartial = (E_ReplaceFlags)(rs.Flag & FormatConfig.E_ReplaceFlagsUCF.Partials) == E_ReplaceFlags.Partials;
bool onlyDoList = (E_ReplaceFlags)(rs.Flag & FormatConfig.E_ReplaceFlagsUCF.BeforeList) == E_ReplaceFlags.BeforeList; // C2021-045 if (!dopartial) // F2021-093: Partials moved into their own method and done first
bool onlyIfFirstWord = (E_ReplaceFlags)(rs.Flag & FormatConfig.E_ReplaceFlagsUCF.FirstWord) == E_ReplaceFlags.FirstWord; // C2021-056 {
bool doInPagelist = (E_ReplaceFlags)(rs.Flag & FormatConfig.E_ReplaceFlagsUCF.PageList) == E_ReplaceFlags.PageList; // B2021-132 bool onlyDoList = (E_ReplaceFlags)(rs.Flag & FormatConfig.E_ReplaceFlagsUCF.BeforeList) == E_ReplaceFlags.BeforeList; // C2021-045
//B2021-132 only do replacewords in paglist if the replaceword pagelist flag is set bool onlyIfFirstWord = (E_ReplaceFlags)(rs.Flag & FormatConfig.E_ReplaceFlagsUCF.FirstWord) == E_ReplaceFlags.FirstWord; // C2021-056
if (_DoReplWordInPageList && !doInPagelist) continue; bool doInPagelist = (E_ReplaceFlags)(rs.Flag & FormatConfig.E_ReplaceFlagsUCF.PageList) == E_ReplaceFlags.PageList; // B2021-132
//B2021-132 only do replacewords in paglist if the replaceword pagelist flag is set
if (_DoReplWordInPageList && !doInPagelist) continue;
// note that the order of this check is important. Check in this order... // note that the order of this check is important. Check in this order...
// background here // background here
if (!shouldReplace.ContainsKey((E_ReplaceFlags)rs.Flag)) if (!shouldReplace.ContainsKey((E_ReplaceFlags)rs.Flag))
{
//int profileDepth2 = ProfileTimer.Push(">>>> Before ShouldReplaceIt");
shouldReplace.Add((E_ReplaceFlags)rs.Flag, ShouldReplaceIt((E_ReplaceFlags)rs.Flag));
//ProfileTimer.Pop(profileDepth2);
}
bool replaceit = shouldReplace[(E_ReplaceFlags)rs.Flag];
if (replaceit)
{
// C2021-045 if the BeforeList ReplaceWords flag is set, only do the replace word if the text ends with a colon
if (onlyDoList && !Text.EndsWith(":"))
replaceit = false; // text does not end with a colon so don't replace this word
// C2021-056 if FirstWord ReplaceWords flag is set, only do if the replace word is the first word in the text
if (onlyIfFirstWord && !Text.StartsWith(rs.ReplaceWord))
replaceit = false;
}
if (!replaceit && _DoReplWordInPageList) replaceit = true; // F2021-053: Do replace words in page list
if (replaceit)
{
if (!dicReplaceRegex.ContainsKey(rs))
{ {
RegexOptions myOptions = (E_ReplaceFlags)(rs.Flag & FormatConfig.E_ReplaceFlagsUCF.CaseInsens) == E_ReplaceFlags.CaseInsens ? RegexOptions.IgnoreCase : RegexOptions.None; //int profileDepth2 = ProfileTimer.Push(">>>> Before ShouldReplaceIt");
if (dopartial) shouldReplace.Add((E_ReplaceFlags)rs.Flag, ShouldReplaceIt((E_ReplaceFlags)rs.Flag));
{ //ProfileTimer.Pop(profileDepth2);
dicReplaceRegex.Add(rs, new Regex(rs.ReplaceWord, myOptions)); }
} bool replaceit = shouldReplace[(E_ReplaceFlags)rs.Flag];
else
if (replaceit)
{
// C2021-045 if the BeforeList ReplaceWords flag is set, only do the replace word if the text ends with a colon
if (onlyDoList && !Text.EndsWith(":"))
replaceit = false; // text does not end with a colon so don't replace this word
// C2021-056 if FirstWord ReplaceWords flag is set, only do if the replace word is the first word in the text
if (onlyIfFirstWord && !Text.StartsWith(rs.ReplaceWord))
replaceit = false;
}
if (!replaceit && _DoReplWordInPageList) replaceit = true; // F2021-053: Do replace words in page list
if (replaceit)
{
if (!dicReplaceRegex.ContainsKey(rs))
{ {
RegexOptions myOptions = (E_ReplaceFlags)(rs.Flag & FormatConfig.E_ReplaceFlagsUCF.CaseInsens) == E_ReplaceFlags.CaseInsens ? RegexOptions.IgnoreCase : RegexOptions.None;
//int profileDepth3 = ProfileTimer.Push(">>>> DoReplaceWords2.BuildMatch"); //int profileDepth3 = ProfileTimer.Push(">>>> DoReplaceWords2.BuildMatch");
// CASEINSENS: Do ReplaceWords for all words that match, regardless of case, and replace with the ReplaceWith string as is // CASEINSENS: Do ReplaceWords for all words that match, regardless of case, and replace with the ReplaceWith string as is
//RegexOptions myOptions = (rs.Flag & E_ReplaceFlags.CaseInsens) == E_ReplaceFlags.CaseInsens ? RegexOptions.IgnoreCase & RegexOptions.Singleline : RegexOptions.None & RegexOptions.Singleline; //RegexOptions myOptions = (rs.Flag & E_ReplaceFlags.CaseInsens) == E_ReplaceFlags.CaseInsens ? RegexOptions.IgnoreCase & RegexOptions.Singleline : RegexOptions.None & RegexOptions.Singleline;
@ -2128,19 +2127,17 @@ namespace VEPROMS.CSLA.Library
dicReplaceRegex.Add(rs, new Regex(pat, myOptions)); dicReplaceRegex.Add(rs, new Regex(pat, myOptions));
//ProfileTimer.Pop(profileDepth3); //ProfileTimer.Pop(profileDepth3);
} }
try
{
//int profileDepth4 = ProfileTimer.Push(">>>> DoReplaceWords2.Partial");
myMatches.Add(dicReplaceRegex[rs], rs);
//ProfileTimer.Pop(profileDepth4);
}
catch (Exception ex)
{
Console.WriteLine("{0},'{1}',{2},'{3}'", _MyItemInfo.ActiveFormat.Name, rs.ReplaceWord, ex.GetType().Name, ex.Message);
}
} }
try
{
//int profileDepth4 = ProfileTimer.Push(">>>> DoReplaceWords2.Partial");
if (!dopartial) myMatches.Add(dicReplaceRegex[rs], rs);
else partialReplaceList.Add(rs, dicReplaceRegex[rs]);
//ProfileTimer.Pop(profileDepth4);
}
catch (Exception ex)
{
Console.WriteLine("{0},'{1}',{2},'{3}'", _MyItemInfo.ActiveFormat.Name, rs.ReplaceWord, ex.GetType().Name, ex.Message);
}
dopartial = false;
} }
} }
//ProfileTimer.Pop(profileDepth); //ProfileTimer.Pop(profileDepth);
@ -2148,14 +2145,71 @@ namespace VEPROMS.CSLA.Library
Text = myMatches.ReplaceMatches(); Text = myMatches.ReplaceMatches();
//ProfileTimer.Pop(profileDepth5); //ProfileTimer.Pop(profileDepth5);
Text = Text.Replace(@"\xA0", @"\u160?"); //replace hard space Text = Text.Replace(@"\xA0", @"\u160?"); //replace hard space
try return Text;
}
// F2021-093: separate out partial replace words so that they can all be done before normal replace words. Partials read in the 'repword', use
// it as-is as a dotnet regular expression to do replacement. Aside from the dotnet regular expression process, the rest of this
// code is similar to plain regular expressions, in terms of processing flags for which steps, etc.
private string DoReplacePartials(string Text, FormatConfig.ReplaceStrData rsl)
{
Dictionary<FormatConfig.ReplaceStr, Regex> partialReplaceList = new Dictionary<FormatConfig.ReplaceStr, Regex>();
Dictionary<E_ReplaceFlags?, bool> shouldReplace = new Dictionary<E_ReplaceFlags?, bool>();
foreach (FormatConfig.ReplaceStr rs in rsl)
{ {
foreach (FormatConfig.ReplaceStr prs in partialReplaceList.Keys) bool dopartial = (E_ReplaceFlags)(rs.Flag & FormatConfig.E_ReplaceFlagsUCF.Partials) == E_ReplaceFlags.Partials;
Text = partialReplaceList[prs].Replace(Text, prs.ReplaceWith); if (dopartial)
} {
catch (Exception ex) // Don't crash on a format issue. bool onlyDoList = (E_ReplaceFlags)(rs.Flag & FormatConfig.E_ReplaceFlagsUCF.BeforeList) == E_ReplaceFlags.BeforeList; // C2021-045
{ bool onlyIfFirstWord = (E_ReplaceFlags)(rs.Flag & FormatConfig.E_ReplaceFlagsUCF.FirstWord) == E_ReplaceFlags.FirstWord; // C2021-056
_MyLog.WarnFormat("{0} - {1}", ex.GetType().Name, ex.Message); bool doInPagelist = (E_ReplaceFlags)(rs.Flag & FormatConfig.E_ReplaceFlagsUCF.PageList) == E_ReplaceFlags.PageList; // B2021-132
//B2021-132 only do replacewords in paglist if the replaceword pagelist flag is set
if (_DoReplWordInPageList && !doInPagelist) continue;
// note that the order of this check is important. Check in this order...
if (!shouldReplace.ContainsKey((E_ReplaceFlags)rs.Flag))
{
shouldReplace.Add((E_ReplaceFlags)rs.Flag, ShouldReplaceIt((E_ReplaceFlags)rs.Flag));
}
bool replaceit = shouldReplace[(E_ReplaceFlags)rs.Flag];
if (replaceit)
{
// C2021-045 if the BeforeList ReplaceWords flag is set, only do the replace word if the text ends with a colon
if (onlyDoList && !Text.EndsWith(":"))
replaceit = false; // text does not end with a colon so don't replace this word
// C2021-056 if FirstWord ReplaceWords flag is set, only do if the replace word is the first word in the text
if (onlyIfFirstWord && !Text.StartsWith(rs.ReplaceWord))
replaceit = false;
}
if (!replaceit && _DoReplWordInPageList) replaceit = true; // F2021-053: Do replace words in page list
if (replaceit)
{
if (!dicReplaceRegex.ContainsKey(rs))
{
RegexOptions myOptions = (E_ReplaceFlags)(rs.Flag & FormatConfig.E_ReplaceFlagsUCF.CaseInsens) == E_ReplaceFlags.CaseInsens ? RegexOptions.IgnoreCase : RegexOptions.None;
dicReplaceRegex.Add(rs, new Regex(rs.ReplaceWord, myOptions));
}
try
{
partialReplaceList.Add(rs, dicReplaceRegex[rs]);
}
catch (Exception ex)
{
Console.WriteLine("{0},'{1}',{2},'{3}'", _MyItemInfo.ActiveFormat.Name, rs.ReplaceWord, ex.GetType().Name, ex.Message);
}
}
}
Text = Text.Replace(@"\xA0", @"\u160?"); //replace hard space
try
{
foreach (FormatConfig.ReplaceStr prs in partialReplaceList.Keys)
Text = partialReplaceList[prs].Replace(Text, prs.ReplaceWith);
//if (partialReplaceList.Count>0) GC.Collect(); // microsoft had a memory leak in regular expression code - this REALLY slows it down though
}
catch (Exception ex) // Don't crash on a format issue.
{
_MyLog.WarnFormat("{0} - {1}", ex.GetType().Name, ex.Message);
}
} }
return Text; return Text;
} }