Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add String::concat_view #1249

Closed
wants to merge 4 commits into from
Closed

Conversation

Lampese
Copy link
Collaborator

@Lampese Lampese commented Nov 26, 2024

@Lampese Lampese linked an issue Nov 26, 2024 that may be closed by this pull request
@coveralls
Copy link
Collaborator

coveralls commented Nov 26, 2024

Pull Request Test Coverage Report for Build 3978

Details

  • 1 of 1 (100.0%) changed or added relevant line in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.003%) to 80.132%

Totals Coverage Status
Change from base Build 3975: 0.003%
Covered Lines: 4360
Relevant Lines: 5441

💛 - Coveralls

@@ -64,6 +64,22 @@ pub fn String::concat(
buf.to_string()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the rationale to make it a plain function istead of a method?

@peter-jerry-ye
Copy link
Collaborator

peter-jerry-ye commented Nov 27, 2024

How about:

pub(open) trait IterString {
  iter(Self) -> Iter[String]
}

pub fn concat[T : IterString](strings : T, separator~ : String = "") -> String {
  let buf = StringBuilder::new()
  let mut str : String? = None
  for s in strings {
    if str.is_empty().not() {
      buf.write_string(str.unwrap())
      buf.write_string(separator)
    }
    str = Some(s)
  } else {
    if str.is_empty().not() {
      buf.write_string(str.unwrap())
    }
  }
  buf.to_string()
}

test {
  inspect!(concat(([] : Array[String])), content="")
  inspect!(concat(["a", "b", "c"]), content="abc")
  inspect!(concat(["a", "b", "c"], separator="|"), content="a|b|c")
  inspect!(concat(["a", "b", "c"][1:], separator="|"), content="b|c")
  inspect!(
    concat((["a", "b", "c"] : FixedArray[String]), separator="|"),
    content="a|b|c",
  )
}

@Lampese
Copy link
Collaborator Author

Lampese commented Nov 27, 2024

How about:

pub(open) trait IterString {
  iter(Self) -> Iter[String]
}

pub fn concat[T : IterString](strings : T, separator~ : String = "") -> String {
  let buf = StringBuilder::new()
  let mut str : String? = None
  for s in strings {
    if str.is_empty().not() {
      buf.write_string(str.unwrap())
      buf.write_string(separator)
    }
    str = Some(s)
  } else {
    if str.is_empty().not() {
      buf.write_string(str.unwrap())
    }
  }
  buf.to_string()
}

test {
  inspect!(concat(([] : Array[String])), content="")
  inspect!(concat(["a", "b", "c"]), content="abc")
  inspect!(concat(["a", "b", "c"], separator="|"), content="a|b|c")
  inspect!(concat(["a", "b", "c"][1:], separator="|"), content="b|c")
  inspect!(
    concat((["a", "b", "c"] : FixedArray[String]), separator="|"),
    content="a|b|c",
  )
}

It is a quite feasible plan

@Lampese Lampese marked this pull request as draft November 27, 2024 02:10
@peter-jerry-ye
Copy link
Collaborator

We are now considering moving it to Iter[String]::join

@Yoorkin
Copy link
Collaborator

Yoorkin commented Nov 27, 2024

String::concat was a mistake. In the self parameter, the types in the type constructor can be a concrete type like String. After discussing offline, we will move to Iter[String]::join and make String::concat a function. The join method will be added later because users can always use other methods instead.

@Lampese
Copy link
Collaborator Author

Lampese commented Nov 27, 2024

@Lampese Lampese closed this Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make String::concat take ArrayView[T]
5 participants