mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 05:25:32 +03:00
Ladybird: Add an AppKit NSString category to hold common utilities
This currently holds a helper to create a new string which collapses all consecutive whitespace to a single ASCII space.
This commit is contained in:
parent
1f07a18f4e
commit
507dea5fdd
Notes:
sideshowbarker
2024-07-17 01:27:18 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/507dea5fdd Pull-request: https://github.com/SerenityOS/serenity/pull/21063 Reviewed-by: https://github.com/ADKaster Reviewed-by: https://github.com/bugaevc
15
Ladybird/AppKit/Utilities/NSString+Ladybird.h
Normal file
15
Ladybird/AppKit/Utilities/NSString+Ladybird.h
Normal file
@ -0,0 +1,15 @@
|
||||
/*
|
||||
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#import <System/Cocoa.h>
|
||||
|
||||
@interface NSString (Ladybird)
|
||||
|
||||
- (NSString*)stringByCollapsingConsecutiveWhitespace;
|
||||
|
||||
@end
|
55
Ladybird/AppKit/Utilities/NSString+Ladybird.mm
Normal file
55
Ladybird/AppKit/Utilities/NSString+Ladybird.mm
Normal file
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/CharacterTypes.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <AK/Utf8View.h>
|
||||
#include <LibUnicode/CharacterTypes.h>
|
||||
|
||||
#import <Utilities/Conversions.h>
|
||||
#import <Utilities/NSString+Ladybird.h>
|
||||
|
||||
static BOOL code_point_is_whitespace(u32 code_point)
|
||||
{
|
||||
static auto white_space_category = Unicode::property_from_string("White_Space"sv);
|
||||
|
||||
if (!white_space_category.has_value())
|
||||
return is_ascii_space(code_point);
|
||||
|
||||
return Unicode::code_point_has_property(code_point, *white_space_category);
|
||||
}
|
||||
|
||||
@implementation NSString (Ladybird)
|
||||
|
||||
- (NSString*)stringByCollapsingConsecutiveWhitespace
|
||||
{
|
||||
auto* result = [NSMutableString string];
|
||||
|
||||
auto const* string = [self UTF8String];
|
||||
Utf8View code_points { StringView { string, strlen(string) } };
|
||||
|
||||
bool currently_skipping_spaces = false;
|
||||
|
||||
for (auto code_point : code_points) {
|
||||
if (code_point_is_whitespace(code_point)) {
|
||||
if (!currently_skipping_spaces) {
|
||||
currently_skipping_spaces = true;
|
||||
[result appendString:@" "];
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
auto code_point_string = String::from_code_point(code_point);
|
||||
[result appendString:Ladybird::string_to_ns_string(code_point_string)];
|
||||
|
||||
currently_skipping_spaces = false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@end
|
@ -142,10 +142,11 @@ elseif (APPLE)
|
||||
AppKit/UI/Tab.mm
|
||||
AppKit/UI/TabController.mm
|
||||
AppKit/Utilities/Conversions.mm
|
||||
AppKit/Utilities/NSString+Ladybird.mm
|
||||
AppKit/Utilities/URL.mm
|
||||
)
|
||||
target_include_directories(ladybird PRIVATE AppKit)
|
||||
target_link_libraries(ladybird PRIVATE ${COCOA_LIBRARY})
|
||||
target_link_libraries(ladybird PRIVATE ${COCOA_LIBRARY} LibUnicode)
|
||||
target_compile_options(ladybird PRIVATE
|
||||
-fobjc-arc
|
||||
-Wno-deprecated-anon-enum-enum-conversion # Required for CGImageCreate
|
||||
|
Loading…
Reference in New Issue
Block a user