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

[SR-15615] [SE-0263] Incorrect buffer count for small capacity? #57911

Closed
benrimmington opened this issue Dec 17, 2021 · 1 comment · Fixed by #67929
Closed

[SR-15615] [SE-0263] Incorrect buffer count for small capacity? #57911

benrimmington opened this issue Dec 17, 2021 · 1 comment · Fixed by #67929
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@benrimmington
Copy link
Collaborator

Previous ID SR-15615
Radar None
Original Reporter @benrimmington
Type Bug
Environment
  • macOS 12.0.1 (21A559)

  • Xcode version 13.2 (13C90)

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee None
Priority Medium

md5: 1a2f74d992daac9b9dd8f4d6500c6f1b

Issue Description:

String.init(unsafeUninitializedCapacity:initializingUTF8With:) has different behavior for a small vs large capacity.

// Example 1. Small strings (capacity <= _SmallString.capacity)
for capacity in 0...15 {
  String.init(unsafeUninitializedCapacity: capacity) { buffer in
    // Always prints "false 16"
    print(capacity == buffer.count, buffer.count)
    return 0
  }
}
// Example 2. Large strings (capacity > _SmallString.capacity)
for capacity in 16...255 {
  String.init(unsafeUninitializedCapacity: capacity) { buffer in
    // Always prints "true ..."
    print(capacity == buffer.count, buffer.count)
    return 0
  }
}

The first example is surprising because:

  • The actual buffer.count of 16 exceeds the _SmallString.capacity of 15.

  • The API documentation suggests the buffer will only have "room for `capacity` UTF-8 code units".

I noticed this issue because I was using buffer.endIndex when initializing in reverse.

@benrimmington
Copy link
Collaborator Author

Cc: @milseman, @Catfish-Man

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant