diff options
author | E.Z. Hart <hartez@users.noreply.github.com> | 2017-01-11 11:33:04 -0700 |
---|---|---|
committer | Jason Smith <jason.smith@xamarin.com> | 2017-01-11 10:33:04 -0800 |
commit | 1d58aaec70266cf8bbbd3f94d2dd55719263eccd (patch) | |
tree | b7529b66e3eea2004c4516894d7e8595a2c6819c /Xamarin.Forms.Platform.Android | |
parent | 11b982e5d556324b344888ddf446b6c044a7954b (diff) | |
download | xamarin-forms-1d58aaec70266cf8bbbd3f94d2dd55719263eccd.tar.gz xamarin-forms-1d58aaec70266cf8bbbd3f94d2dd55719263eccd.tar.bz2 xamarin-forms-1d58aaec70266cf8bbbd3f94d2dd55719263eccd.zip |
Don't create FontExtensions Regex unless/until necessary (#676)
* Lazy load Regex for loading fonts from assets
* Allow Regex class to handle Regex initialization and caching
Diffstat (limited to 'Xamarin.Forms.Platform.Android')
-rw-r--r-- | Xamarin.Forms.Platform.Android/Renderers/FontExtensions.cs | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/Xamarin.Forms.Platform.Android/Renderers/FontExtensions.cs b/Xamarin.Forms.Platform.Android/Renderers/FontExtensions.cs index d7394a10..981065b7 100644 --- a/Xamarin.Forms.Platform.Android/Renderers/FontExtensions.cs +++ b/Xamarin.Forms.Platform.Android/Renderers/FontExtensions.cs @@ -10,7 +10,10 @@ namespace Xamarin.Forms.Platform.Android { static readonly Dictionary<Tuple<string, FontAttributes>, Typeface> Typefaces = new Dictionary<Tuple<string, FontAttributes>, Typeface>(); - static readonly Regex LoadFromAssets = new Regex(@"\w+\.((ttf)|(otf))\#\w*"); + // We don't create and cache a Regex object here because we may not ever need it, and creating Regexes is surprisingly expensive (especially on older hardware) + // Instead, we'll use the static Regex.IsMatch below, which will create and cache the regex internally as needed. It's the equivalent of Lazy<Regex> with less code. + // See https://msdn.microsoft.com/en-us/library/sdx2bds0(v=vs.110).aspx#Anchor_2 + const string LoadFromAssetsRegex = @"\w+\.((ttf)|(otf))\#\w*"; static Typeface s_defaultTypeface; @@ -56,7 +59,7 @@ namespace Xamarin.Forms.Platform.Android var style = ToTypefaceStyle(self.FontAttributes); result = Typeface.Create(Typeface.Default, style); } - else if (LoadFromAssets.IsMatch(self.FontFamily)) + else if (Regex.IsMatch(self.FontFamily, LoadFromAssetsRegex)) { result = Typeface.CreateFromAsset(AApplication.Context.Assets, FontNameToFontFile(self.FontFamily)); } @@ -88,7 +91,7 @@ namespace Xamarin.Forms.Platform.Android var style = ToTypefaceStyle(self.FontAttributes); result = Typeface.Create(Typeface.Default, style); } - else if (LoadFromAssets.IsMatch(self.FontFamily)) + else if (Regex.IsMatch(self.FontFamily, LoadFromAssetsRegex)) { result = Typeface.CreateFromAsset(AApplication.Context.Assets, FontNameToFontFile(self.FontFamily)); } |