Parse YogaValue from string. inverse of toString()

Reviewed By: kittens

Differential Revision: D5120456

fbshipit-source-id: 6ac7cff2a040778e63a953070e1bd7e768fedaa7
This commit is contained in:
Emil Sjolander 2017-05-24 07:47:54 -07:00 committed by Facebook Github Bot
parent 0b4e772e3b
commit e656adcaa8

View File

@ -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);
}
}