From f68b50bb4bc215edc45a10fda70a51028286f77e Mon Sep 17 00:00:00 2001 From: Emil Sjolander Date: Wed, 24 May 2017 07:47:54 -0700 Subject: [PATCH] Parse YogaValue from string. inverse of toString() Summary: Implement the inverse of toString. This allows us to parse a YogaValue from a string inputted from the user in debugging tools. Reviewed By: kittens Differential Revision: D5120456 fbshipit-source-id: 6ac7cff2a040778e63a953070e1bd7e768fedaa7 --- java/com/facebook/yoga/YogaValue.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/java/com/facebook/yoga/YogaValue.java b/java/com/facebook/yoga/YogaValue.java index 5065d115..2affc937 100644 --- a/java/com/facebook/yoga/YogaValue.java +++ b/java/com/facebook/yoga/YogaValue.java @@ -15,6 +15,7 @@ import com.facebook.proguard.annotations.DoNotStrip; public class YogaValue { static final YogaValue UNDEFINED = new YogaValue(YogaConstants.UNDEFINED, YogaUnit.UNDEFINED); static final YogaValue ZERO = new YogaValue(0, YogaUnit.POINT); + static final YogaValue AUTO = new YogaValue(YogaConstants.UNDEFINED, YogaUnit.AUTO); public final float value; public final YogaUnit unit; @@ -60,4 +61,24 @@ public class YogaValue { throw new IllegalStateException(); } } + + public static YogaValue parse(String s) { + if (s == null) { + return null; + } + + if ("undefined".equals(s)) { + return UNDEFINED; + } + + if ("auto".equals(s)) { + return AUTO; + } + + if (s.endsWith("%")) { + return new YogaValue(Float.parseFloat(s.substring(0, s.length() - 1)), YogaUnit.PERCENT); + } + + return new YogaValue(Float.parseFloat(s), YogaUnit.POINT); + } }