collect bytes from string - arrays

This is decoded string from bytes, they are always different. Am not using it in the code, its just for shown what is all about.
"Random String; Tags:Value1:1,Value:2,Value3:value4"
This is array of bytes from above string which i get as input.
[&u8...&u8]
What i need is get the values fromthose. While every byte in the array is changing. but some bytes are always same. I was thinking if there is any way how to extract it without using any Strings... Thanks for any ideas
so the output would look like this:
let v1 = [&u8, &u8, &u8, &u8, &u8];
let v2 = [&u8, &u8];
let v3 = [&u8];
let v4 = [&u8];
let v5 = [&u8];

You can do all this without allocating any extra space using rusts iterators, using split and related functions
At the top level, your data is of the form (key:value;)*
This suggests first splitting on ;
Then splitting each of these pieces into key and value using :
In your case, all the information is when the key is "tags".
Then within the tags section, you again have (mostly) key-value pairs of the form (key-value,)* so we need to split on , then break into key-value pairs using -.
An example that does this but only prints all the tag key-value pairs is:
fn split_kv(v: &[u8], c: u8) -> Option<(&[u8], &[u8])> {
let n = v.iter().position(|&b| b == c)?;
let w = v.split_at(n);
Some((w.0, &(w.1)[1..]))
}
fn main() {
let s: &str = "Background:Sunfire Topo;Base:Eagle;Accessory3:None;Patch:Oreo;Jacket:Pink Bonez;Eyes:BloodShot;Beak:Drool;Accessory2:Nose Ring;Accessory1:None;Item:Knife;tags:Dope Eagles,ELEMENT-HYDRO,ATTACK-10,DEFENSE-5,HIGHNESS-4,SWAG-1;metadata:QmU7JcFDoGcUvNkDgsPz9cy13md4xHdNyD6itwmgVLuo7x/860.json";
let ss: &[u8] = s.as_bytes();
let tags = ss
.split(|&b| b == b';') // Break up at ';'
.filter_map(|s| split_kv(s, b':')) // Split each piece into key-value pairs using ':'
.filter_map(|(k, v)| { // Only keep the tags entry.
if k == "tags".as_bytes() {
Some(v)
} else {
None
}
})
.next() // And just take the first of those.
.unwrap();
// Split the tags by ','
for t in tags.split(|&b| b == b',') {
// Then try to convert each to a key-value using '-' as seperator.
if let Some((k, v)) = split_kv(t, b'-') {
println!(
"k={:?} v={:?}",
std::str::from_utf8(k).unwrap(),
std::str::from_utf8(v).unwrap()
);
} else {
println!("t={:?}", std::str::from_utf8(t).unwrap());
}
}
}
You can run this here

Related

Splitting string into array string.components(separtedBy: ",") consumes more time

I have text file which contains 18000 lines which have cities names. Each line has city name, state, latitude, longitude etc. Below is the function which does that, if i don't implement string.components(separtedBy: ", ") loading function is pretty fast but with it implemented it takes time which makes my UI freeze. What is the right way of doing it? Is string.components(separtedBy: ", ") that costly?
I profiled the app, this line is taking string.components(separtedBy: ", ") 1.45s out of 2.09s in whole function.
func readCitiesFromCountry(country: String) -> [String] {
var cityArray: [String] = []
var flag = true
var returnedCitiesList: [String] = []
if let path = Bundle.main.path(forResource: country, ofType: "txt") {
guard let streamReader = StreamReader(path: path) else {fatalError()}
defer {
streamReader.close()
}
while flag {
if let nextLine = streamReader.nextLine() {
cityArray = nextLine.components(separatedBy: ",") // this is the line taking a lot of time, without this function runs pretty fast
if (country == "USA") {
returnedCitiesList.append("\(cityArray[0]) , \(cityArray[1]) , \(cityArray[2])")
} else {
returnedCitiesList.append("\(cityArray[0]) , \(cityArray[1])")
}
//returnedCitiesList.append(nextLine)
} else {
flag = false
}
}
} else {
fatalError()
}
return returnedCitiesList
}
StreamReader used in the code can be found here. It helps to read file line by line
Read a file/URL line-by-line in Swift
This question is not about how to split the string into array Split a String into an array in Swift? , rather why splitting is taking more time in the given function.
NSString.components(separatedBy:) returns a [String], which requires that all of the pieces' content be copied, from the original string, and pasted into new-ly allocated stringss. This slows things down.
You could address the symptoms (UI freezing) by putting this work on a background thread, but that just sweeps the problem under the wrong (the inefficient copying is still there), and complicates things (async code is never fun).
Instead, you should consider using String.split(separator:maxSplits:omittingEmptySubsequences:), which returns [Substring]. Each Substring is just a view into the original string's memory, which stores the relevant range so that you only see that portion of the String which is modeled by the Substring. The only memory allocation happening here is for the array.
Hopefully that should be enough to speed your code up to acceptable levels. If not, you should combine both solutions, and use split off-thread.

Refer to array based on string output

I'm learning Swift as I go here, so apologies if this is a silly question.
I'm looking to use the output of one function (String) to determine an input into a different function (Array).
The first function output (String) is then combined with another String to form the name of an already defined Array, which i'd like to use as input to second function. However, despite having the same name, the String is not seen as an array.
I've skipped some of the code, but relevant section below.
// Defined array
let rushProb = [0,11,19,64,78,89,96,98,99,100]
// Define probability and outcome function - PlayType
func findPlay(prob: [Int], outcome: [String]) -> String {
if let index = prob.firstIndex(where: { $0 > Int.random(in: 1...100) }) {
return outcome[index]
}
else {
return "na"
}
}
// This is successfully output as "rush"
let playSel = findPlay(prob: scen1Prob, outcome: scenPlay)
// This then creates "rushProb"
let playSelProb = playSel+"Prob"
// I want this to ultimately be findYards(prob: rushProb)
findYards(prob: playSelProb)
Well, you could use a dictionary where the key replaces your array name, and the value is the array. Then, you'd use the name you create to look up the array value in the dictionary:
let arrays = ["rushProb": [0,11,19,64,78,89,96,98,99,100],
"fooProb" : [0,15,29,44,68,78,86,92,94,100]]
// This is successfully output as "rush"
let playSel = findPlay(prob: scen1Prob, outcome: scenPlay)
// This then creates "rushProb"
let playSelProb = playSel+"Prob"
// look up the array that corresponds to "rushProb"
if let array = arrays[playSelProb] {
findYards(prob: array)
}

Map Core Data model object to make calculations

I am trying to iterate through a Core Data model object and make calculations as follow. I can't figure out the for - in loops tho. Here every value is multiplied by every amount so the append and total are wrong. I only need each value to be multiplied to it's corresponding amount (for example bitcoin with 1.00000000).
func updateWalletLabel() {
var values : [Double] = []
guard let items : [CryptosMO] = CoreDataHandler.fetchObject() else { return }
let codes = items.map { $0.code! }
let amounts = items.map { $0.amount! }
print(codes) // ["bitcoin", "litecoin"]
print(amounts) // ["1.00000000", "2.00000000"]
// please note these values are in correct order (x1 bitcoin, x2 litecoin)
for code in codes {
for amount in amounts {
let convertedAmount = Double(amount)!
let crypto = code
guard let price = CryptoInfo.cryptoPriceDic[crypto] else { return }
let calculation = price * convertedAmount
values.append(calculation)
}
}
let total = values.reduce(0.0, { $0 + Double($1) } )
print("VALUES", values) // [7460.22, 14920.44, 142.68, 285.36] (need [7460.22, 285.36])
print("TOTAL:", total) // 22808.7 (need 7745.58)
}
How can I modify my for-in loops here so the calculations are only happening once for each array item?
Thanks!
When you have two arrays of the same length and in the same order, you can use Swift's zip function to combine the two into an array of tuples. In that case, your loop would change to
for (code, amount) in zip(codes, amounts) {
// Your calculation
}
See also the documentation

How do I move String values from an array to a tuple without copying?

I have a fixed size array of Strings: [String; 2]. I want to turn it into a (String, String). Can I do this without copying the values?
The piece of code that I'm working on in particular is the following:
let (basis, names_0, names_1) = if let Some(names) = self.arg_name {
(ComparisonBasis::Name, names[0], names[1])
} else {
(ComparisonBasis::File, self.arg_file[0], self.arg_file[1])
};
types:
self.arg_name: Option<[String; 2]>
self.arg_file: Vec<String>
Right now I'm getting errors
cannot move out of type `[std::string::String; 2]`, a non-copy fixed-size array [E0508]
and
cannot move out of indexed content [E0507]
for the two arms of the if
You've omitted a fair amount of context, so I'm taking a guess at a few aspects. I'm also hewing a little closer to the question you asked, rather than the vaguer one implied by your snippets.
struct NeverSpecified {
arg_names: Option<[String; 2]>,
arg_file: Vec<String>,
}
impl NeverSpecified {
fn some_method_i_guess(mut self) -> (String, String) {
if let Some(mut names) = self.arg_names {
use std::mem::replace;
let name_0 = replace(&mut names[0], String::new());
let name_1 = replace(&mut names[1], String::new());
(name_0, name_1)
} else {
let mut names = self.arg_file.drain(0..2);
let name_0 = names.next().expect("expected 2 names, got 0");
let name_1 = names.next().expect("expected 2 names, got 1");
(name_0, name_1)
}
}
}
I use std::mem::replace to switch the contents of the array, whilst leaving it in a valid state. This is necessary because Rust won't allow you to have a "partially valid" array. There are no copies or allocations involved in this path.
In the other path, we have to pull elements out of the vector by hand. Again, you can't just move values out of a container via indexing (this is actually a limitation of indexing overall). Instead, I use Vec::drain to essentially chop the first two elements out of the vector, then extract them from the resulting iterator. To be clear: this path doesn't involve any copies or allocations, either.
As an aside, those expect methods shouldn't ever be triggered (since drain does bounds checking), but better paranoid than sorry; if you want to replace them with unwrap() calls instead, that should be fine..
Since Rust 1.36, you can use slice patterns to bind to all the values of the array at once:
struct NeverSpecified {
arg_names: Option<[String; 2]>,
arg_file: Vec<String>,
}
impl NeverSpecified {
fn some_method_i_guess(mut self) -> (String, String) {
if let Some([name_0, name_1]) = self.arg_names.take() {
(name_0, name_1)
} else {
let mut names = self.arg_file.drain(0..2);
let name_0 = names.next().expect("expected 2 names, got 0");
let name_1 = names.next().expect("expected 2 names, got 1");
(name_0, name_1)
}
}
}
See also:
Method for safely moving all elements out of a generic array into a tuple with minimal overhead

Swift array loop once, write many

Consider the following silly, simple example:
let arr = ["hey", "ho"]
let doubled = arr.map {$0 + $0}
let capitalized = arr.map {$0.capitalizedString}
As you can see, I'm processing the same initial array in multiple ways in order to end up with multiple processed arrays.
Now imagine that arr is very long and that I have many such processes generating many final arrays. I don't like the above code because we are looping multiple times, once for each map call. I'd prefer to loop just once.
Now, obviously we could handle this by brute force, i.e. by starting with multiple mutable arrays and writing into all of them on each iteration:
let arr = ["hey", "ho"]
var doubled = [String]()
var capitalized = [String]()
for s in arr {
doubled.append(s + s)
capitalized.append(s.capitalizedString)
}
Fine. But now we don't get the joy of using map. So my question is: is there a better, Swiftier way? In a hazy way I imagine myself using map, or something like map, to generate something like a tuple and magically splitting that tuple out into all resulting arrays as we iterate, as if I could say something like this (pseudocode, don't try this at home):
let arr = ["hey", "ho"]
let (doubled, capitalized) = arr.map { /* ???? */ }
If I were designing my own language, I might even permit a kind of splatting by assignment into a pseudo-array of lvalues:
let arr = ["hey", "ho"]
let [doubled, capitalized] = arr.map { /* ???? */ }
No big deal if it can't be done, but it would be fun to be able to talk this way.
How about a function, multimap, that takes a collection of transformations, and applies each one, returning them as an array of arrays:
// yay protocol extensions
extension SequenceType {
// looks like T->U works OK as a constraint
func multimap
<U, C: CollectionType
where C.Generator.Element == Generator.Element->U>
(transformations: C) -> [[U]] {
return transformations.map {
self.map($0)
}
}
}
Then use it like this:
let arr = ["hey", "ho"]
let double: String->String = { $0 + $0 }
let uppercase: String->String = { $0.uppercaseString }
arr.multimap([double, uppercase])
// returns [["heyhey", "hoho"], ["HEY", "HO"]]
Or it might be quite nice in variadic form:
extension SequenceType {
func multimap<U>(transformations: (Generator.Element->U)...) -> [[U]] {
return self.multimap(transformations)
}
}
arr.multimap({ $0 + $0 }, { $0.uppercaseString })
Edit: if you want separate variables, I think the best you can do is a destructure function (which you have to declare n times for each n-tuple unfortunately):
// I don't think this can't be expressed as a protocol extension quite yet
func destructure<C: CollectionType>(source: C) -> (C.Generator.Element,C.Generator.Element) {
precondition(source.count == 2)
return (source[source.startIndex],source[source.startIndex.successor()])
}
// and, since it's a function, let's declare pipe forward
// to make it easier to call
infix operator |> { }
func |> <T,U>(lhs: T, rhs: T->U) -> U {
return rhs(lhs)
}
And then you can declare the variables like this:
let (doubled,uppercased)
= arr.multimap({ $0 + $0 }, { $0.uppercaseString }) |> destructure
Yes this is a teensy bit inefficient because you have to build the array then rip it apart – but that’s really not going to be material, since the arrays are copy-on-write and we’re talking about a small number of them in the outer array.
edit: an excuse to use the new guard statement:
func destructure<C: Sliceable where C.SubSlice.Generator.Element == C.Generator.Element>(source: C) -> (C.Generator.Element,C.Generator.Element) {
guard let one = source.first else { fatalError("empty source") }
guard let two = dropFirst(source).first else { fatalError("insufficient elements") }
return (one,two)
}
What is wrong with your suggestion of tuple?
let arr = ["hey", "ho"]
let mapped = arr.map {e in
return (e + e, e.capitalizedString)
}
How about this, we process 'capitalized' array while we map the 'doubled' array:
let arr = ["hey", "ho"]
var capitalized = [String]()
let doubled = arr.map {(var myString) -> String in
capitalized.append(myString.capitalizedString)
return myString + myString
}
//doubled ["heyhey", "hoho"]
//capitalized: ["Hey", "Ho"]

Resources