Preface
Adding features or syntax sugar to text strings is common in various programming languages. Take the C language that everyone is familiar with for example. A C string is essentially a character array, but every time you enter a string, you don’t need to enter ['h','e','l','l','o'], just hit hello, because the compiler does this operation for you.
More advanced languages such as Swift handles strings not just as character arrays, String is a complete type and has various characteristics. Let’s first look at a feature of String: substring.
Take a look at String
First, let’s take a rough understanding of the implementation of strings. The following code comes from String.swift in the standard library:
public struct String { public var _core: _StringCore}Of course there are some other initialization settings, but there is only this storage attribute in the declaration! The secret must be in StringCore.swift:
public struct _StringCore { public var _baseAddress: UnsafeMutableRawPointer? var _countAndFlags: UInt public var _owner: AnyObject?}There are many other things in this type, but we only focus on storage properties:
The real situation of _StringCore is much more complicated than mentioned here, but the above content makes it easier to understand some information about strings: the internal storage and size of strings.
Substring
How to create a substring in Swift? The easiest way is to take a paragraph from string by subscript:
let str = "Hello Swift!"let slice = str[str.startIndex..<str index="" str="" startindex="" nbsp="" offsetby:="" 5="" hello=""></str>
Although it's simple, the code doesn't look too elegant.
The index of String is not an intuitive integer, so the position index during intercepting needs to be obtained using startIndex and index(_:offsetBy:). If you intercept from the starting position of the string, you can omit startIndex:
let withPartialRange = str[..<str index="" str="" startindex="" nbsp="" offsetby:="" 5="" still="" hello=""></str>
Or use this method in collection:
let slice = str.prefix(5)// still "Hello"
Remember that strings are also collections, so you can use methods under collections, such as prefix(), suffix(), dropFirst(), etc.
The internal principles of Substring
Substring A magical place is that they reuse the memory of the parent string. You can understand substring as one of the paragraphs of the parent string.
For example, if you cut 100 characters from an 8000 character string, you do not need to reinitialize the 100 character memory space.
This also means that you may accidentally extend the life cycle of the parent string. If there is a large string, then you just intercept a small segment, as long as the intercepted small segment string is not released, the large segment string will not be released.
How is Substring done?
public struct Substring { internal var _slice: RangeReplaceableBidirectionalSlice<string></string>The internal _slice attribute holds all information about the parent string:
// Still inside Substringinternal var _wholeString: String { return _slice._base}public var startIndex: Index { return _slice.startIndex }public var endIndex: Index { return _slice.endIndex }Compute the property _wholeString (returns the entire parent string), startIndex and endIndex are both returned through the internal _slice.
It can also be seen how slice refers to the parent string.
Substring to String
Finally, there may be many substrings in the code, but the parameter type of the function requires string. The process of converting Substring to string is also very simple:
let string = String(substring)
Because substrings shares the same memory space as its parent string, it is guessed that creating a new string should initialize a new storage space. So what is the process of initializing string?
extension String { public init(_ substring: Substring) { // 1 let x = substring._wholeString // 2 let start = substring.startIndex let end = substring.endIndex // 3 let u16 = x._core[start.encodedOffset..<end encodedoffset="" nbsp="" 4a="" if="" start="" sameposition="" in:="" x="" unicodescalars="" end="" self="" 4b="" else=""></end>The steps to convert substring to string are very simple, but you may want to consider whether you need to do this. Do you require the type to be string when performing substring operations? If all operations on substring need to be converted into string, then the lightweight substring will lose its meaning.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.