Skip to content

Commit

Permalink
reduce duplicate IndexOf calls (#12599)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivalik authored Feb 15, 2023
1 parent a63c5cd commit 0f4ee16
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ internal void RemoveWindow(Window window)
if (window is Element windowElement)
{
var oldIndex = InternalChildren.IndexOf(windowElement);
InternalChildren.Remove(windowElement);
InternalChildren.RemoveAt(oldIndex);
windowElement.Parent = null;
OnChildRemoved(windowElement, oldIndex);
}
Expand Down
6 changes: 4 additions & 2 deletions src/Controls/src/Core/ListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,10 +456,12 @@ protected override void UnhookContent(Cell content)
{
base.UnhookContent(content);

if (content == null || !_logicalChildren.Contains(content))
if (content == null)
return;
var index = _logicalChildren.IndexOf(content);
_logicalChildren.Remove(content);
if (index == -1)
return;
_logicalChildren.RemoveAt(index);
content.Parent = null;
VisualDiagnostics.OnChildRemoved(this, content, index);

Expand Down
6 changes: 3 additions & 3 deletions src/Controls/src/Core/Shell/Shell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -713,11 +713,11 @@ public void RemoveLogicalChild(Element element)

element.Parent = null;

if (!_logicalChildren.Contains(element))
var oldLogicalIndex = _logicalChildren.IndexOf(element);
if (oldLogicalIndex == -1)
return;

var oldLogicalIndex = _logicalChildren.IndexOf(element);
_logicalChildren.Remove(element);
_logicalChildren.RemoveAt(oldLogicalIndex);
OnChildRemoved(element, oldLogicalIndex);
VisualDiagnostics.OnChildRemoved(this, element, oldLogicalIndex);
}
Expand Down
8 changes: 5 additions & 3 deletions src/Controls/src/Core/VisualElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,12 @@ static void OnTransformChanged(BindableObject bindable, object oldValue, object
var transforms = ((string)newValue).Split(' ');
foreach (var transform in transforms)
{
if (string.IsNullOrEmpty(transform) || transform.IndexOf("(", StringComparison.Ordinal) < 0 || transform.IndexOf(")", StringComparison.Ordinal) < 0)
var openBracket = transform.IndexOf("(", StringComparison.Ordinal);
var closeBracket = transform.IndexOf(")", StringComparison.Ordinal);
if (string.IsNullOrEmpty(transform) || openBracket < 0 || closeBracket < 0)
throw new FormatException("Format for transform is 'none | transform(value) [transform(value) ]*'");
var transformName = transform.Substring(0, transform.IndexOf("(", StringComparison.Ordinal));
var value = transform.Substring(transform.IndexOf("(", StringComparison.Ordinal) + 1, transform.IndexOf(")", StringComparison.Ordinal) - transform.IndexOf("(", StringComparison.Ordinal) - 1);
var transformName = transform.Substring(0, openBracket);
var value = transform.Substring(openBracket + 1, closeBracket - openBracket - 1);
double translationX, translationY, scaleX, scaleY, rotateX, rotateY, rotate;
if (transformName.StartsWith("translateX", StringComparison.OrdinalIgnoreCase) && double.TryParse(value, out translationX))
bindable.SetValue(TranslationXProperty, translationX);
Expand Down
5 changes: 3 additions & 2 deletions src/Controls/src/Xaml/TypeArgumentsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ static XmlType Parse(string match, ref string remaining, IXmlNamespaceResolver r
IList<XmlType> typeArguments = null;
if (isGeneric)
{
var openBracket = type.IndexOf("(", StringComparison.Ordinal);
typeArguments = ParseExpression(
type.Substring(type.IndexOf("(", StringComparison.Ordinal) + 1, type.LastIndexOf(")", StringComparison.Ordinal) - type.IndexOf("(", StringComparison.Ordinal) - 1), resolver, lineinfo);
type = type.Substring(0, type.IndexOf("(", StringComparison.Ordinal));
type.Substring(openBracket + 1, type.LastIndexOf(")", StringComparison.Ordinal) - openBracket - 1), resolver, lineinfo);
type = type.Substring(0, openBracket);
}

var split = type.Split(':');
Expand Down
5 changes: 3 additions & 2 deletions src/Controls/src/Xaml/XmlTypeXamlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ static class XmlTypeXamlExtensions
for (var i = 0; i < lookupNames.Count; i++)
{
var name = lookupNames[i];
if (name.IndexOf(":", StringComparison.Ordinal) != -1)
name = name.Substring(name.LastIndexOf(':') + 1);
var lastIndex = name.LastIndexOf(":", StringComparison.Ordinal);
if (lastIndex != -1)
name = name.Substring(lastIndex + 1);
if (typeArguments != null)
name += "`" + typeArguments.Count; //this will return an open generic Type
lookupNames[i] = name;
Expand Down
8 changes: 4 additions & 4 deletions src/Core/src/Handlers/View/ViewHandlerOfT.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ protected override void SetupContainer()
return;

var oldParent = (Panel?)PlatformView.Parent;

var oldIndex = oldParent?.Children.IndexOf(PlatformView);
oldParent?.Children.Remove(PlatformView);
if (oldIndex is int oldIdx && oldIdx >= 0)
oldParent?.Children.RemoveAt(oldIdx);

ContainerView ??= new WrapperView();
((WrapperView)ContainerView).Child = PlatformView;
Expand All @@ -43,9 +43,9 @@ protected override void RemoveContainer()
}

var oldParent = (Panel?)ContainerView.Parent;

var oldIndex = oldParent?.Children.IndexOf(ContainerView);
oldParent?.Children.Remove(ContainerView);
if (oldIndex is int oldIdx && oldIdx >= 0)
oldParent?.Children.RemoveAt(oldIdx);

CleanupContainerView(ContainerView);
ContainerView = null;
Expand Down

0 comments on commit 0f4ee16

Please sign in to comment.