SIL.FieldWorks.FwCoreDlgs.ReplaceAllCollectorEnv.ReplaceString C# (CSharp) Method

ReplaceString() public static method

Replaces the string.
public static ReplaceString ( ITsStrBldr tsbBuilder, ITsString tssInput, int ichMinInput, int ichLimInput, ITsString tssReplace, int delta, bool fEmptySearch, bool fUseWs ) : int
tsbBuilder ITsStrBldr The string builder for the text to be replaced.
tssInput ITsString The input string to be replaced.
ichMinInput int The start in the input string.
ichLimInput int The lim in the input string.
tssReplace ITsString The replacement text. This should come from VwPattern.ReplacementText, /// NOT VwPattern.ReplaceWith. The former includes any ORCs that need to be saved from the input, as well as /// properly handling $1, $2 etc. in regular expressions.
delta int length difference between tssInput and tsbBuilder from previous /// replacements.
fEmptySearch bool true if search text is empty (irun.e. we're searching /// for a style or Writing System)
fUseWs bool if set to true use the writing system used in the /// replace string of the Find/Replace dialog.
return int
		public static int ReplaceString(ITsStrBldr tsbBuilder, ITsString tssInput,
			int ichMinInput, int ichLimInput, ITsString tssReplace, int delta, bool fEmptySearch,
			bool fUseWs)
		{
			int initialLength = tsbBuilder.Length;
			int replaceRunCount = tssReplace.RunCount;

			// Determine whether to replace the sStyleName. We do this if any of the runs of
			// the replacement string have the sStyleName set (to something other than
			// Default Paragraph Characters).
			bool fUseStyle = false;
			bool fUseTags = false;

			// ENHANCE (EberhardB): If we're not doing a RegEx search we could store these flags
			// since they don't change.
			TsRunInfo runInfo;
			for (int irunReplace = 0; irunReplace < replaceRunCount; irunReplace++)
			{
				ITsTextProps textProps = tssReplace.FetchRunInfo(irunReplace, out runInfo);
				string sStyleName =
					textProps.GetStrPropValue((int)FwTextPropType.ktptNamedStyle);
				if (sStyleName != null && sStyleName.Length > 0)
					fUseStyle = true;

				//string tags = textProps.GetStrPropValue((int)FwTextPropType.ktptTags);
				//if (tags.Length > 0)
				//    fUseTags = true;
			}

			int iRunInput = tssInput.get_RunAt(ichMinInput);
			ITsTextProps selProps = tssInput.get_Properties(iRunInput);
			ITsPropsBldr propsBldr = selProps.GetBldr();

			// Remove all tags that are anywhere in the Find-what string. But also include any
			// other tags that are present in the first run of the found string. So the resulting
			// replacement string will have any tags in the first char of the selection plus
			// any specified replacement tags.
			//			Vector<StrUni> vstuTagsToRemove;
			//			GetTagsToRemove(m_qtssFindWhat, &fUseTags, vstuTagsToRemove);
			//			Vector<StrUni> vstuTagsToInclude;
			//			GetTagsToInclude(qtssSel, vstuTagsToRemove, vstuTagsToInclude);

			// Make a string builder to accumulate the real replacement string.

			// Copy the runs of the replacement string, adjusting the properties.
			// Make a string builder to accumulate the real replacement string.
			ITsStrBldr stringBldr = TsStrBldrClass.Create();

			// Copy the runs of the replacement string, adjusting the properties.
			for (int irun = 0; irun < replaceRunCount; irun++)
			{
				ITsTextProps ttpReplaceRun = tssReplace.FetchRunInfo(irun, out runInfo);
				if (TsStringUtils.GetGuidFromRun(tssReplace, irun) != Guid.Empty)
				{
					// If the run was a footnote or picture ORC, then just use the run
					// properties as they are.
				}
				else if (fUseWs || fUseStyle || fUseTags)
				{
					// Copy only writing system/old writing system, char sStyleName and/or
					// tag info into the builder.
					if (fUseWs)
					{
						int ttv, ws;
						ws = ttpReplaceRun.GetIntPropValues((int)FwTextPropType.ktptWs, out ttv);
						propsBldr.SetIntPropValues((int)FwTextPropType.ktptWs, ttv, ws);
					}
					if (fUseStyle)
					{
						string sStyleName = ttpReplaceRun.GetStrPropValue(
							(int)FwTextPropType.ktptNamedStyle);

						if (sStyleName == FwStyleSheet.kstrDefaultCharStyle)
							propsBldr.SetStrPropValue((int)FwTextPropType.ktptNamedStyle,
								null);
						else
							propsBldr.SetStrPropValue((int)FwTextPropType.ktptNamedStyle,
								sStyleName);
					}
					//if (fUseTags)
					//{
					//    string sTagsRepl = ttpReplaceRun.GetStrPropValue(ktptTags);
					//    string sTags = AddReplacementTags(vstuTagsToInclude, sTagsRepl);
					//    propsBldr.SetStrPropValue(ktptTags, sTags);
					//}
					ttpReplaceRun = propsBldr.GetTextProps();
				}
				else
				{
					// Its not a footnote so copy all props exactly from (the first run of the) matched text.
					ttpReplaceRun = selProps;
				}

				// Insert modified run into string builder.
				if (fEmptySearch && tssReplace.Length == 0)
				{
					// We are just replacing an ws/ows/sStyleName/tags. The text remains unchanged.
					// ENHANCE (SharonC): Rework this when we get patterns properly implemented.
					string runText = tssInput.get_RunText(iRunInput);
					if (runText.Length > ichLimInput - ichMinInput)
						runText = runText.Substring(0, ichLimInput - ichMinInput);
					stringBldr.Replace(0, 0, runText, ttpReplaceRun);
				}
				else
				{
					stringBldr.Replace(runInfo.ichMin, runInfo.ichMin,
						tssReplace.get_RunText(irun), ttpReplaceRun);
				}
			}

			tsbBuilder.ReplaceTsString(delta + ichMinInput, delta + ichLimInput, stringBldr.GetString());
			int finalLength = tsbBuilder.Length;
			return finalLength - initialLength;
		}
		#endregion