Skip to content

Commit

Permalink
Fabric: Conversion functions between NSString and std::string
Browse files Browse the repository at this point in the history
Summary:
@public
Trivial. It's also nice this we have a default encoding now.

Reviewed By: fkgozali

Differential Revision: D8528919

fbshipit-source-id: 0853eca828f22ead1a337fea3d7a2fc9a48e84c8
  • Loading branch information
shergin authored and facebook-github-bot committed Jun 22, 2018
1 parent 93b568c commit 3ea4a33
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ - (void)updateProps:(SharedProps)props

// `nativeId`
if (oldViewProps.nativeId != newViewProps.nativeId) {
self.nativeId = [NSString stringWithCString:newViewProps.nativeId.c_str()
encoding:NSASCIIStringEncoding];
self.nativeId = RCTNSStringFromString(newViewProps.nativeId);
}
}
}

Expand Down Expand Up @@ -165,7 +165,7 @@ - (BOOL)accessibilityPerformMagicTap

- (BOOL)didActivateAccessibilityCustomAction:(UIAccessibilityCustomAction *)action
{
_eventEmitter->onAccessibilityAction([action.name cStringUsingEncoding:NSASCIIStringEncoding]);
_eventEmitter->onAccessibilityAction(RCTStringFromNSString(action.name));
return YES;
}

Expand Down
7 changes: 3 additions & 4 deletions React/Fabric/Mounting/RCTMountingManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#import "RCTMountItemProtocol.h"

#import "RCTCreateMountItem.h"
#import "RCTConversions.h"
#import "RCTDeleteMountItem.h"
#import "RCTInsertMountItem.h"
#import "RCTRemoveMountItem.h"
Expand Down Expand Up @@ -46,8 +47,7 @@ - (void)mutateComponentViewTreeWithMutationInstructions:(facebook::react::TreeMu
for (auto instruction : instructions) {
switch (instruction.getType()) {
case TreeMutationInstruction::Creation: {
NSString *componentName = [NSString stringWithCString:instruction.getNewChildNode()->getComponentName().c_str()
encoding:NSASCIIStringEncoding];
NSString *componentName = RCTNSStringFromString(instruction.getNewChildNode()->getComponentName(), NSASCIIStringEncoding);
RCTCreateMountItem *mountItem =
[[RCTCreateMountItem alloc] initWithComponentName:componentName
tag:instruction.getNewChildNode()->getTag()];
Expand All @@ -56,8 +56,7 @@ - (void)mutateComponentViewTreeWithMutationInstructions:(facebook::react::TreeMu
}

case TreeMutationInstruction::Deletion: {
NSString *componentName = [NSString stringWithCString:instruction.getOldChildNode()->getComponentName().c_str()
encoding:NSASCIIStringEncoding];
NSString *componentName = RCTNSStringFromString(instruction.getOldChildNode()->getComponentName(), NSASCIIStringEncoding);
RCTDeleteMountItem *mountItem =
[[RCTDeleteMountItem alloc] initWithComponentName:componentName
tag:instruction.getOldChildNode()->getTag()];
Expand Down
8 changes: 8 additions & 0 deletions React/Fabric/RCTConversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
#import <fabric/graphics/Geometry.h>
#import <fabric/view/primitives.h>

inline NSString *_Nullable RCTNSStringFromString(const std::string &string, const NSStringEncoding &encoding = NSUTF8StringEncoding) {
return [NSString stringWithCString:string.c_str() encoding:encoding];
}

inline std::string RCTStringFromNSString(NSString *string, const NSStringEncoding &encoding = NSUTF8StringEncoding) {
return [string cStringUsingEncoding:encoding];
}

inline UIColor *_Nullable RCTUIColorFromSharedColor(const facebook::react::SharedColor &sharedColor) {
return sharedColor ? [UIColor colorWithCGColor:sharedColor.get()] : nil;
}
Expand Down
2 changes: 1 addition & 1 deletion React/Fabric/RCTScheduler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void schedulerDidComputeMutationInstructions(Tag rootTag, const TreeMutationInst

void schedulerDidRequestPreliminaryViewAllocation(ComponentName componentName) override {
RCTScheduler *scheduler = (__bridge RCTScheduler *)scheduler_;
[scheduler.delegate schedulerDidRequestPreliminaryViewAllocationWithComponentName:[NSString stringWithCString:componentName.c_str() encoding:NSASCIIStringEncoding]];
[scheduler.delegate schedulerDidRequestPreliminaryViewAllocationWithComponentName:RCTNSStringFromString(componentName, NSASCIIStringEncoding)];
}

private:
Expand Down

0 comments on commit 3ea4a33

Please sign in to comment.